rsnext/examples/with-passport/lib/user.js
Luis Alvarez D 7d42b07b79
[Example] with-passport (#10529)
* Added basic layout with login page

* Updated styles

* Added form component

* Added signup page

* Added login/signup API endpoints

* Bug fixes

* Set the cookie

* Added logout route

* Added more auth

* Updated signup

* Added profile page

* Added useUser

* Fix link

* Updated redirect path

* Renaming some files

* Added README

* Apply suggestions from Shu

Co-Authored-By: Shu Uesugi <shu@chibicode.com>

* Add useUser to the header

Co-authored-by: Shu Uesugi <shu@chibicode.com>
2020-03-19 11:40:13 -05:00

27 lines
1 KiB
JavaScript

// 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() }
}