// AuthenticateUser attempts to log in an existing user with the provided
// credentials, returning an access token if the credentials match.
AuthenticateUser attempts to log in an existing user with the provided
credentials, returning an access token if the credentials match.
// AuthenticateUser attempts to log in an existing user with the provided // credentials returning an access token if the credentials match .
func (lm *LoginManager) AuthenticateUser(login UserLogin, ctx *dispatch.Context) (err error) {
	existing := SavedUser{}
	err = lm.DB.Table("users").GetObject(login.Username, &existing)
	if err != nil {
		return err
	}
	valid := CheckPassword(login.Password, existing.HashedPassword)
	if valid {
		token, err := lm.Token.CreateToken(login.Username)
		if err != nil {
			return err
		}
		authCookie := &http.Cookie{
			Name:  "dispatch-auth",
			Value: token,
			HttpOnly: true,
			SameSite: http.SameSiteStrictMode,
			MaxAge:   int(time.Duration(time.Hour * 24 * 30).Seconds()), 
		}
		ctx.Writer.Header().Add("Set-Cookie", authCookie.String())
		loggedInCookie := &http.Cookie{
			Name:     "dispatch-logged-in",
			Value:    "true",
			Path:     "/",
			MaxAge:   int(time.Duration(time.Hour * 24 * 30).Seconds()), 
			SameSite: http.SameSiteNoneMode,
		}
		ctx.Writer.Header().Add("Set-Cookie", loggedInCookie.String())
		return nil
	}
	return ErrorIncorrectLogin
}
func ( lm * LoginManager ) AuthenticateUser ( login UserLogin , ctx * dispatch . Context ) ( err error ) { existing := SavedUser { } err = lm . DB . Table ( " " ) . GetObject ( login . Username , & existing ) if err != nil { return err } valid := CheckPassword ( login . Password , existing . HashedPassword ) if valid { token , err := lm . Token . CreateToken ( login . Username ) if err != nil { return err } authCookie := & http . Cookie { Name : " " , Value : token , HttpOnly : true , SameSite : http . SameSiteStrictMode , MaxAge : int ( time . Duration ( time . Hour * 24 * 30 ) . Seconds ( ) ) , } ctx . Writer . Header ( ) . Add ( " " , authCookie . String ( ) ) loggedInCookie := & http . Cookie { Name : " " , Value : " " , Path : " " , MaxAge : int ( time . Duration ( time . Hour * 24 * 30 ) . Seconds ( ) ) , SameSite : http . SameSiteNoneMode , } ctx . Writer . Header ( ) . Add ( " " , loggedInCookie . String ( ) ) return nil } return ErrorIncorrectLogin }
AuthenticateUser attempts to log in an existing user with the provided
credentials, returning an access token if the credentials match.
AuthenticateUser attempts to log in an existing user with the provided credentials returning an access token if the credentials match .
// No error; login successful // Secure: true, // 30 days // 30 days