Example #1
0
func (lilo *basicLoginLogout) HandleLogout(request shared.IRequest) (shared.IResponse, error) {
	//request.Session.shared.IUser = nil
	log.Println("LOGOUT")
	request.ResetSession()
	request.Session().AddFlash("success", "Logged Out")
	return getRedirectResponse("/")
}
Example #2
0
func (lilo *basicLoginLogout) doLogin(request shared.IRequest, noPassword bool, usernameCol string, username interface{}, password string) {

	doError := func(verboseMessage string, err error) {
		log.Printf("Issue loggin in (not error): %s, U:%s", verboseMessage, username)

		if err != nil {
			log.Printf("Error loading user '%s' from database: %s\n", username, err.Error())
		}
		if noPassword {
			request.Session().AddFlash("error", verboseMessage)
		} else {
			request.Session().AddFlash("error", "The presented credentials were not matched. Please try again.")
		}
		request.Redirect("/login")
	}

	db := lilo.db

	rows, err := db.Query(`SELECT `+lilo.userColString()+` FROM `+lilo.usersTable+` WHERE `+usernameCol+` = ?`, username)
	if err != nil {
		panic(err)
		log.Fatal(err)
		return
	}

	defer rows.Close()

	canHaz := rows.Next()
	if !canHaz {
		if noPassword {
			doError(fmt.Sprintf("Could not find %s", username), nil)
		} else {
			doError("", nil)
		}
		return
	}

	user, err := lilo.LoadUser(rows)
	if err != nil {
		doError("Invalid user identifier", err)
		return
	}

	if !noPassword {
		log.Printf("Check Password")
		res, err := user.CheckPassword(password)
		if err != nil {
			doError("", err)
			return
		}
		if !res {
			doError("", err)
			return
		}
	}

	target := "/app.html"
	//if request.Session.LoginTarget != nil {
	//		target = *request.Session.LoginTarget
	//	}

	request.ResetSession()

	request.Session().SetUser(user)
	if user.SetOnNextLogin() {
		request.Redirect("/set_password")
	} else {
		request.Redirect(target)
	}

	log.Printf("Done Check Password")
}