Skip to content

Commit 760ff75

Browse files
committed
Create users, validate credentials
1 parent d4017e0 commit 760ff75

4 files changed

Lines changed: 61 additions & 16 deletions

File tree

examples/serverless-auth-example/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020
"dependencies": {
2121
"@types/aws-lambda": "^8.10.72",
2222
"@types/crypto-js": "^4.0.1",
23+
"@types/lodash.omit": "^4.5.6",
2324
"aws-lambda": "^1.0.6",
2425
"crypto-js": "^4.0.0",
26+
"lodash.omit": "^4.5.0",
2527
"simple-dynamodb": "^1.0.1",
2628
"typescript": "^4.1.5",
2729
"uuid": "^8.3.2"

examples/serverless-auth-example/serverless.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,10 @@ resources:
4040
KeyType: HASH
4141
ProvisionedThroughput:
4242
ReadCapacityUnits: 1
43-
WriteCapacityUntis: 1
43+
WriteCapacityUnits: 1
4444
TableName: ${self:provider.environment.USER_TABLE}
4545

4646
package:
4747
exclude:
4848
- node_modules/typescript/**
49+
- node_modules/@types/**
Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
11
import { APIGatewayEvent } from "aws-lambda"
22
import * as db from "simple-dynamodb"
33
import sha256 from "crypto-js/sha256"
4+
import omit from "lodash.omit"
45

56
function response(statusCode: number, body: any) {
6-
return { statusCode, body: JSON.stringify(body) }
7+
return {
8+
statusCode,
9+
// permissive CORS headers
10+
headers: {
11+
"Access-Control-Allow-Headers": "Content-Type",
12+
"Access-Control-Allow-Origin": "*",
13+
"Access-Control-Allow-Methods": "OPTIONS,POST,GET",
14+
},
15+
body: JSON.stringify(body),
16+
}
717
}
818

919
// Hashing your password before saving is critical
@@ -15,6 +25,21 @@ function hashPassword(username: string, password: string) {
1525
).toString()
1626
}
1727

28+
async function createUser(username: string, password: string) {
29+
const result = await db.updateItem({
30+
TableName: process.env.USER_TABLE!,
31+
Key: {
32+
username,
33+
},
34+
UpdateExpression: `SET password = :password, createdAt = :createdAt`,
35+
ExpressionAttributeValues: {
36+
":password": hashPassword(username, password),
37+
":createdAt": new Date().toISOString(),
38+
},
39+
})
40+
return result.Attributes
41+
}
42+
1843
// Logs you in based on username/password combo
1944
// Creates user on first login
2045
export const login = async (event: APIGatewayEvent) => {
@@ -27,30 +52,30 @@ export const login = async (event: APIGatewayEvent) => {
2752
})
2853
}
2954

30-
// username is the key, which means it must be unique
31-
let user = await db.getItem({
55+
// find user in database
56+
let { Item: user } = await db.getItem({
3257
TableName: process.env.USER_TABLE!,
3358
Key: {
59+
// username is the key, which means it must be unique
3460
username,
3561
},
3662
})
3763

3864
if (!user) {
3965
// user was not found, create
40-
user = await db.updateItem({
41-
TableName: process.env.USER_TABLE!,
42-
Key: {
43-
username,
44-
},
45-
UpdateExpression: `SET password = :password, createdAt = :createdAt`,
46-
ExpressionAttributeValues: {
47-
":password": hashPassword(username, password),
48-
":createdAt": new Date().toISOString(),
49-
},
50-
})
66+
user = await createUser(username, password)
5167
} else {
5268
// check credentials
69+
if (hashPassword(username, password) !== user.password) {
70+
// 🚨
71+
return response(401, {
72+
status: "error",
73+
error: "Bad username/password combination",
74+
})
75+
}
5376
}
5477

55-
return response(200, user)
78+
// user was created or has valid credentials
79+
80+
return response(200, omit(user, "password"))
5681
}

examples/serverless-auth-example/yarn.lock

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@
1212
resolved "https://registry.yarnpkg.com/@types/crypto-js/-/crypto-js-4.0.1.tgz#3a4bd24518b0e6c5940da4e2659eeb2ef0806963"
1313
integrity sha512-6+OPzqhKX/cx5xh+yO8Cqg3u3alrkhoxhE5ZOdSEv0DOzJ13lwJ6laqGU0Kv6+XDMFmlnGId04LtY22PsFLQUw==
1414

15+
"@types/lodash.omit@^4.5.6":
16+
version "4.5.6"
17+
resolved "https://registry.yarnpkg.com/@types/lodash.omit/-/lodash.omit-4.5.6.tgz#f2a9518259e481a48ff7ec423420fa8fd58933e2"
18+
integrity sha512-KXPpOSNX2h0DAG2w7ajpk7TXvWF28ZHs5nJhOJyP0BQHkehgr948RVsToItMme6oi0XJkp19CbuNXkIX8FiBlQ==
19+
dependencies:
20+
"@types/lodash" "*"
21+
22+
"@types/lodash@*":
23+
version "4.14.168"
24+
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.168.tgz#fe24632e79b7ade3f132891afff86caa5e5ce008"
25+
integrity sha512-oVfRvqHV/V6D1yifJbVRU3TMp8OT6o6BG+U9MkwuJ3U8/CsDHvalRpsxBqivn71ztOFZBTfJMvETbqHiaNSj7Q==
26+
1527
argparse@^1.0.7:
1628
version "1.0.10"
1729
resolved "https://registry.yarnpkg.com/argparse/-/argparse-1.0.10.tgz#bcd6791ea5ae09725e17e5ad988134cd40b3d911"
@@ -116,6 +128,11 @@ js-yaml@^3.13.1:
116128
argparse "^1.0.7"
117129
esprima "^4.0.0"
118130

131+
lodash.omit@^4.5.0:
132+
version "4.5.0"
133+
resolved "https://registry.yarnpkg.com/lodash.omit/-/lodash.omit-4.5.0.tgz#6eb19ae5a1ee1dd9df0b969e66ce0b7fa30b5e60"
134+
integrity sha1-brGa5aHuHdnfC5aeZs4Lf6MLXmA=
135+
119136
punycode@1.3.2:
120137
version "1.3.2"
121138
resolved "https://registry.yarnpkg.com/punycode/-/punycode-1.3.2.tgz#9653a036fb7c1ee42342f2325cceefea3926c48d"

0 commit comments

Comments
 (0)