Skip to content

Commit 638ffea

Browse files
author
Max
authored
chore(heroku): Adds Heroku (#2)
1 parent 524da6e commit 638ffea

File tree

17 files changed

+267
-136
lines changed

17 files changed

+267
-136
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@
1010

1111
# Output of the go coverage tool, specifically when used with LiteIDE
1212
*.out
13-
src/build
13+
build

Procfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
web: bin/golang-cognito-example

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Golang AWS Cognito Register, Verify phone number, Login and Get User example
22

3+
## Just Show Me
4+
5+
If you are just curious how things work all together, you can find this example working at https://golang-cognito-example.herokuapp.com
6+
7+
## Instructions
8+
39
This example code demonstrates how to use AWS Cognito with AWS Go SDK in a form of simple web pages where you can:
410

511
1. Check if username is taken
@@ -13,12 +19,10 @@ You will also need to create App Client in User Pool without "Generate Secret Ke
1319

1420
## Build
1521

16-
Go to `/src` folder and run:
17-
1822
```go
1923
go build -o ./build/cognito
2024

21-
AWS_PROFILE=XXX COGNITO_APP_CLIENT_ID=XXX COGNITO_USER_POOL_ID=XXX ./build/cognito
25+
AWS_PROFILE=XXX COGNITO_APP_CLIENT_ID=XXX COGNITO_USER_POOL_ID=XXX PORT=8080 ./build/cognito
2226
```
2327

24-
Visit http://localhost:8080/register to see the registration page.
28+
Visit http://localhost:8080/ to see the list of available pages.

app/app.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package app
2+
3+
import (
4+
cognito "github.com/aws/aws-sdk-go/service/cognitoidentityprovider"
5+
)
6+
7+
// App holds internals for auth flow.
8+
type App struct {
9+
CognitoClient *cognito.CognitoIdentityProvider
10+
UserPoolID string
11+
AppClientID string
12+
}

src/login.go renamed to app/login.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package app
22

33
import (
44
"fmt"
@@ -13,7 +13,7 @@ const flowUsernamePassword = "USER_PASSWORD_AUTH"
1313
const flowRefreshToken = "REFRESH_TOKEN_AUTH"
1414

1515
// Login handles login scenario.
16-
func (c *CognitoExample) Login(w http.ResponseWriter, r *http.Request) {
16+
func (a *App) Login(w http.ResponseWriter, r *http.Request) {
1717
r.ParseForm()
1818

1919
username := r.Form.Get("username")
@@ -37,13 +37,13 @@ func (c *CognitoExample) Login(w http.ResponseWriter, r *http.Request) {
3737
authTry := &cognito.InitiateAuthInput{
3838
AuthFlow: flow,
3939
AuthParameters: params,
40-
ClientId: aws.String(c.AppClientID),
40+
ClientId: aws.String(a.AppClientID),
4141
}
4242

43-
res, err := c.CognitoClient.InitiateAuth(authTry)
43+
res, err := a.CognitoClient.InitiateAuth(authTry)
4444
if err != nil {
4545
fmt.Println(err)
46-
http.Redirect(w, r, fmt.Sprintf("/login?error=%s", err.Error()), http.StatusSeeOther)
46+
http.Redirect(w, r, fmt.Sprintf("/login?message=%s", err.Error()), http.StatusSeeOther)
4747
return
4848
}
4949

src/otp.go renamed to app/otp.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package app
22

33
import (
44
"fmt"
@@ -9,21 +9,22 @@ import (
99
)
1010

1111
// OTP handles phone verification step.
12-
func (c *CognitoExample) OTP(w http.ResponseWriter, r *http.Request) {
12+
func (a *App) OTP(w http.ResponseWriter, r *http.Request) {
1313
r.ParseForm()
1414

1515
otp := r.Form.Get("otp")
16+
username := r.Form.Get("username")
1617

1718
user := &cognito.ConfirmSignUpInput{
1819
ConfirmationCode: aws.String(otp),
19-
Username: aws.String(c.RegFlow.Username),
20-
ClientId: aws.String(c.AppClientID),
20+
Username: aws.String(username),
21+
ClientId: aws.String(a.AppClientID),
2122
}
2223

23-
_, err := c.CognitoClient.ConfirmSignUp(user)
24+
_, err := a.CognitoClient.ConfirmSignUp(user)
2425
if err != nil {
2526
fmt.Println(err)
26-
http.Redirect(w, r, fmt.Sprintf("/otp?error=%s", err.Error()), http.StatusSeeOther)
27+
http.Redirect(w, r, fmt.Sprintf("/otp?message=%s", err.Error()), http.StatusSeeOther)
2728
return
2829
}
2930

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package app
22

33
import (
44
"fmt"
@@ -9,7 +9,7 @@ import (
99
)
1010

1111
// Register handles sign in scenario.
12-
func (c *CognitoExample) Register(w http.ResponseWriter, r *http.Request) {
12+
func (a *App) Register(w http.ResponseWriter, r *http.Request) {
1313
r.ParseForm()
1414

1515
username := r.Form.Get("username")
@@ -19,7 +19,7 @@ func (c *CognitoExample) Register(w http.ResponseWriter, r *http.Request) {
1919
user := &cognito.SignUpInput{
2020
Username: aws.String(username),
2121
Password: aws.String(password),
22-
ClientId: aws.String(c.AppClientID),
22+
ClientId: aws.String(a.AppClientID),
2323
UserAttributes: []*cognito.AttributeType{
2424
{
2525
Name: aws.String("phone_number"),
@@ -28,15 +28,12 @@ func (c *CognitoExample) Register(w http.ResponseWriter, r *http.Request) {
2828
},
2929
}
3030

31-
_, err := c.CognitoClient.SignUp(user)
31+
_, err := a.CognitoClient.SignUp(user)
3232
if err != nil {
3333
fmt.Println(err)
34-
http.Redirect(w, r, fmt.Sprintf("/register?error=%s", err.Error()), http.StatusSeeOther)
34+
http.Redirect(w, r, fmt.Sprintf("/register?message=%s", err.Error()), http.StatusSeeOther)
3535
return
3636
}
3737

38-
c.RegFlow.Username = username
39-
40-
http.Redirect(w, r, "/otp", http.StatusFound)
41-
38+
http.Redirect(w, r, fmt.Sprintf("/otp?username=%s", username), http.StatusFound)
4239
}

src/username.go renamed to app/username.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package app
22

33
import (
44
"fmt"
@@ -11,12 +11,12 @@ import (
1111
)
1212

1313
// Username handles username scenario.
14-
func (c *CognitoExample) Username(w http.ResponseWriter, r *http.Request) {
14+
func (a *App) Username(w http.ResponseWriter, r *http.Request) {
1515
r.ParseForm()
1616
username := r.Form.Get("username")
1717

18-
_, err := c.CognitoClient.AdminGetUser(&cognito.AdminGetUserInput{
19-
UserPoolId: aws.String(c.UserPoolID),
18+
_, err := a.CognitoClient.AdminGetUser(&cognito.AdminGetUserInput{
19+
UserPoolId: aws.String(a.UserPoolID),
2020
Username: aws.String(username),
2121
})
2222

@@ -25,7 +25,7 @@ func (c *CognitoExample) Username(w http.ResponseWriter, r *http.Request) {
2525
if ok {
2626
if awsErr.Code() == cognito.ErrCodeUserNotFoundException {
2727
m := fmt.Sprintf("Username %s is free!", username)
28-
http.Redirect(w, r, fmt.Sprintf("/username?error=%s", m), http.StatusSeeOther)
28+
http.Redirect(w, r, fmt.Sprintf("/username?message=%s", m), http.StatusSeeOther)
2929
return
3030
}
3131
} else {
@@ -35,5 +35,5 @@ func (c *CognitoExample) Username(w http.ResponseWriter, r *http.Request) {
3535
}
3636

3737
m := fmt.Sprintf("Username %s is taken.", username)
38-
http.Redirect(w, r, fmt.Sprintf("/username?error=%s", m), http.StatusSeeOther)
38+
http.Redirect(w, r, fmt.Sprintf("/username?message=%s", m), http.StatusSeeOther)
3939
}

src/go.mod renamed to go.mod

File renamed without changes.

src/go.sum renamed to go.sum

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
github.com/aws/aws-sdk-go v1.31.14 h1:uRC2riabEXPMHl1CDylsfCod5DKjiOSXhYvxg/Eb9V8=
22
github.com/aws/aws-sdk-go v1.31.14/go.mod h1:5zCpMtNQVjRREroY7sYe8lOMRSxkhG6MZveU8YkpAk0=
3+
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
34
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
45
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
56
github.com/jmespath/go-jmespath v0.3.0 h1:OS12ieG61fsCg5+qLJ+SsW9NicxNkg3b25OyT2yCeUc=
67
github.com/jmespath/go-jmespath v0.3.0/go.mod h1:9QtRXoHjLGCJ5IBSaohpXITPlowMeeYCZ7fLUTSywik=
78
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
9+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
810
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
911
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
12+
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
1013
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
1114
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
15+
golang.org/x/net v0.0.0-20200202094626-16171245cfb2 h1:CCH4IOTTfewWjGOlSp+zGcjutRKlBEZQ6wTn8ozI/nI=
1216
golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
1317
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
18+
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
1419
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
20+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
1521
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
22+
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
1623
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

0 commit comments

Comments
 (0)