forked from vercel/next.js
/
user.js
27 lines (22 loc) · 1.03 KB
/
user.js
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
// import crypto from 'crypto'
/**
* User methods. The example doesn't contain a DB, but for real applications you must use a
* db here, such as MongoDB, Fauna, SQL, etc.
*/
export async function createUser({ username, password }) {
// Here you should create the user and save the salt and hashed password (some dbs may have
// authentication methods that will do it for you so you don't have to worry about it):
//
// const salt = crypto.randomBytes(16).toString('hex')
// const hash = crypto.pbkdf2Sync(password, salt, 1000, 64, 'sha512').toString('hex')
// const user = await DB.createUser({ username, salt, hash })
return { username, createdAt: Date.now() }
}
export async function findUser({ username, password }) {
// Here you should lookup for the user in your DB and compare the password:
//
// const user = await DB.findUser(...)
// const hash = crypto.pbkdf2Sync(password, user.salt, 1000, 64, 'sha512').toString('hex')
// const passwordsMatch = user.hash === hash
return { username, createdAt: Date.now() }
}