|
@ -2,14 +2,30 @@ import Application from "@server/Application"; |
|
|
import { FastifyInstance } from "fastify"; |
|
|
import { FastifyInstance } from "fastify"; |
|
|
import bcrypt from "bcrypt"; |
|
|
import bcrypt from "bcrypt"; |
|
|
import { RegisterToken, User } from "@server/database/entities"; |
|
|
import { RegisterToken, User } from "@server/database/entities"; |
|
|
import RegisterRequest, {IRegisterFormBody} from "../requests/RegisterRequest"; |
|
|
|
|
|
|
|
|
import LoginRequest, { ILoginFormBody } from "../requests/LoginRequest"; |
|
|
|
|
|
import RegisterRequest, { IRegisterFormBody } from "../requests/RegisterRequest"; |
|
|
import handle from "../requests"; |
|
|
import handle from "../requests"; |
|
|
|
|
|
import jwt from "jsonwebtoken"; |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* Register authentication routes |
|
|
* Register authentication routes |
|
|
*/ |
|
|
*/ |
|
|
export default function register(server: FastifyInstance, app: Application) { |
|
|
export default function register(server: FastifyInstance, app: Application) { |
|
|
|
|
|
|
|
|
|
|
|
// Login ---------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
server.post("/auth/login", handle([LoginRequest], async (request, reply) => { |
|
|
|
|
|
let body = <ILoginFormBody>request.body; |
|
|
|
|
|
let user = await User.findOne({ email: body.email }); |
|
|
|
|
|
if (user === undefined || !(await bcrypt.compare(body.password, user.password))) { |
|
|
|
|
|
reply.status(401); |
|
|
|
|
|
reply.send({ "status": "unauthorized" }); |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
let token = jwt.sign({ id: (<User>user).id }, app.APP_KEY, { expiresIn: 60*60*24 }); |
|
|
|
|
|
reply.send({ "status": "success" }); |
|
|
|
|
|
})); |
|
|
|
|
|
|
|
|
// Registration --------------------------------------------------------------------------------
|
|
|
// Registration --------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
@ -23,6 +39,7 @@ export default function register(server: FastifyInstance, app: Application) { |
|
|
user.email = body.email.trim(); |
|
|
user.email = body.email.trim(); |
|
|
user.password = await bcrypt.hash(body.password, 8); |
|
|
user.password = await bcrypt.hash(body.password, 8); |
|
|
await user.save(); |
|
|
await user.save(); |
|
|
|
|
|
await RegisterToken.delete({token: body.token }); |
|
|
reply.send({ status: "success" }); |
|
|
reply.send({ status: "success" }); |
|
|
})); |
|
|
})); |
|
|
|
|
|
|
|
|