Skip to content

Instantly share code, notes, and snippets.

@fulviodenza
Created January 18, 2022 13:41
Show Gist options
  • Save fulviodenza/ffb915dae6b39312628d7c614cc9850b to your computer and use it in GitHub Desktop.
Save fulviodenza/ffb915dae6b39312628d7c614cc9850b to your computer and use it in GitHub Desktop.
func (a *App) Login(c echo.Context) (err error) {
u := new(User)
if err = c.Bind(u); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
}
if err = c.Validate(u); err != nil {
return err
}
params := map[string]*string{
"USERNAME": aws.String(u.Username),
"PASSWORD": aws.String(u.Password),
}
secretHash := computeSecretHash(a.AppClientSecret, u.Username, a.AppClientID)
params["SECRET_HASH"] = aws.String(secretHash)
authTry := &cognito.InitiateAuthInput{
AuthFlow: aws.String("USER_PASSWORD_AUTH"),
AuthParameters: map[string]*string{
"USERNAME": aws.String(*params["USERNAME"]),
"PASSWORD": aws.String(*params["PASSWORD"]),
"SECRET_HASH": aws.String(*params["SECRET_HASH"]),
},
ClientId: aws.String(a.AppClientID), // this is the app client ID
}
authResp, err := a.CognitoClient.InitiateAuth(authTry)
if err != nil {
return c.JSON(http.StatusInternalServerError, authResp)
}
a.Token = *authResp.AuthenticationResult.AccessToken
return c.JSON(http.StatusOK, authResp)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment