KL
Size: a a a
KL
M
s
AM
AM
M
M
M
M
M
AM
const jwt = require('jsonwebtoken')
function getUserId(ctx) {
const Authorization = ctx.request.get('Authorization')
if (Authorization) {
const token = Authorization.replace('Bearer ', '')
const { userId } = jwt.verify(token, "bbapi")
return userId
}
throw new AuthError()
}
class AuthError extends Error {
constructor() {
super('Not authorized')
}
}
module.exports = {
getUserId,
AuthError
}
M
AM
type Mutation {
registerCustomer(email: String!, password: String!, login: String!): AuthPayload!
registerOwner(email: String!, password: String!, login: String!): AuthPayload!
login(email: String!, password: String!): AuthPayload!
}
type AuthPayload {
token: String!
user: User!
}
AM
M
M
AM
const bcrypt = require('bcryptjs')
const jwt = require('jsonwebtoken')
const registerCustomer = async (_, { email, login, password }, ctx, info) => {
const passwordHash = await bcrypt.hash(password, 10)
const user = await ctx.db.mutation.createUser({
data: {
email,
login,
password: passwordHash
}
})
return {
token: jwt.sign({ userId: user.id }, "bbapi"),
user
}
}
M