-
Notifications
You must be signed in to change notification settings - Fork 0
/
create.go
77 lines (71 loc) · 1.96 KB
/
create.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package servicecognito
import (
"context"
"fmt"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/cognitoidentityprovider"
"github.com/aws/aws-sdk-go-v2/service/cognitoidentityprovider/types"
)
type CreateUserData struct {
Username string `json:"username"`
TemporaryPassword string `json:"temporaryPassword"`
CompanyKey string `json:"companyKey"`
StoreKey string `json:"storeKey"`
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
Role string `json:"role"`
}
func CreateUser(ctx context.Context, data *CreateUserData) error {
c, err := getClient(ctx)
if err != nil {
return err
}
attrs := []types.AttributeType{
{
Name: aws.String(cognitoUserAttributeCompany),
Value: aws.String(data.CompanyKey),
},
{
Name: aws.String(cognitoUserAttributeStore),
Value: aws.String(data.StoreKey),
},
{
Name: aws.String(cognitoUserAttributeEmail),
Value: aws.String(data.Username),
},
{
Name: aws.String(cognitoUserAttributeEmailVerified),
Value: aws.String("true"),
},
{
Name: aws.String(cognitoUserAttributeFirstName),
Value: aws.String(data.FirstName),
},
{
Name: aws.String(cognitoUserAttributeLastName),
Value: aws.String(data.LastName),
},
{
Name: aws.String(cognitoUserAttributeName),
Value: aws.String(fmt.Sprintf("%s %s", data.FirstName, data.LastName)),
},
}
_, err = c.AdminCreateUser(ctx, &cognitoidentityprovider.AdminCreateUserInput{
UserPoolId: aws.String(cognitoPool),
Username: aws.String(data.Username),
TemporaryPassword: aws.String(data.TemporaryPassword),
UserAttributes: attrs,
})
if err != nil {
return err
}
_, err = c.AdminAddUserToGroup(ctx, &cognitoidentityprovider.AdminAddUserToGroupInput{
UserPoolId: aws.String(cognitoPool),
Username: aws.String(data.Username),
GroupName: aws.String(data.Role),
})
if err != nil {
return err
}
return nil
}