-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.js
33 lines (29 loc) · 865 Bytes
/
auth.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
28
29
30
31
32
33
import { verify } from 'jsonwebtoken'
import { propOr } from 'ramda'
import { genSalt, hash } from 'bcryptjs'
// just checks if the token is valid
export const authenticate = async (token) => {
if (!token) return null
try {
const payload = (await verify(token, propOr('secret for test', 'JWT_SECRET')(process.env)))
return payload
} catch (e) {
return null
}
}
export const testPassword = async (password, username) => {
if (!/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[^ ]{8,}$/.test(password) || password.includes(username)) {
return false
}
return true
}
export const hashPassword = async (password, username) => {
try {
if (!await testPassword(password, username)) return false
const salt = await genSalt()
const hashedPassword = await hash(password, salt)
return hashedPassword
} catch (err) {
return false
}
}