New controllers and services; Auth
This commit is contained in:
parent
2fc0f9c404
commit
d17f37749d
35 changed files with 1040 additions and 164 deletions
|
|
@ -4,7 +4,7 @@ import jwt from "jsonwebtoken";
|
|||
import type { User, Session } from "../../generated/prisma/client.js";
|
||||
import { getDB } from "../../store/store.js";
|
||||
import type { IUserLogin, IUserRegistration } from "./types.js";
|
||||
import { getJwtSecret } from "../../helpers.js";
|
||||
import { getJwtSecret } from "./helpers.js";
|
||||
|
||||
const registerUser = async (
|
||||
registration: IUserRegistration,
|
||||
|
|
@ -38,16 +38,27 @@ const loginUser = async (login: IUserLogin): Promise<Session | null> => {
|
|||
const user = await getDB().user.findUnique({
|
||||
where: { username: login.username },
|
||||
});
|
||||
|
||||
const passwordCorrect = await argon2.verify(
|
||||
user?.passwordHash ?? "",
|
||||
login.password,
|
||||
);
|
||||
|
||||
if (!user || !passwordCorrect) {
|
||||
if (!user || !user.passwordHash) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const passwordCorrect = await argon2.verify(
|
||||
user.passwordHash,
|
||||
login.password,
|
||||
);
|
||||
if (!passwordCorrect) {
|
||||
return null;
|
||||
}
|
||||
|
||||
await getDB().user.update({
|
||||
data: {
|
||||
lastLogin: new Date(),
|
||||
},
|
||||
where: {
|
||||
id: user.id,
|
||||
},
|
||||
});
|
||||
|
||||
return await getDB().session.create({
|
||||
data: {
|
||||
token: createToken(user.id),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue