Skip to content

Commit

Permalink
feat: install types and create a login services and use in the regist…
Browse files Browse the repository at this point in the history
…er services
  • Loading branch information
Michael-Liendo committed Jul 16, 2024
1 parent 6b8ffee commit 9471930
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 18 deletions.
58 changes: 52 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"dependencies": {
"@fastify/cors": "^8.4.1",
"@linx/shared": "^0.0.1",
"@types/jsonwebtoken": "^9.0.6",
"bcrypt": "^5.1.1",
"dotenv": "^16.3.1",
"fastify": "^4.24.3",
Expand All @@ -38,7 +39,8 @@
"jsonwebtoken": "^9.0.2",
"knex": "^3.0.1",
"pg": "^8.11.3",
"validator": "^13.11.0"
"validator": "^13.11.0",
"zustand": "^4.5.4"
},
"devDependencies": {
"@biomejs/biome": "^1.3.3",
Expand Down
2 changes: 1 addition & 1 deletion server/src/controllers/Auth/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default async function register(request: Request, reply: Reply) {
password,
});

return reply.code(201).send({ message: 'User created', data: { user } });
return reply.code(201).send({ message: 'User created', data: { ...user } });
} catch (error) {
return reply.code(500).send({
message: 'Internal server error',
Expand Down
33 changes: 26 additions & 7 deletions server/src/services/Auth.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,34 @@
import { User } from '../repository/User';
import { hashPassword } from '../utils/password';
import { comparePassword, hashPassword } from '../utils/password';
import Repository from '../repository';
import { BadRequestError } from '../utils/errorHandler';
import { BadRequestError, UnauthorizedError } from '../utils/errorHandler';

import type { IUserForLogin, IUserForRegister } from '@linx/shared';
import { Jwt } from '../utils/jwt';

export default class Auth {
// biome-ignore lint/correctness/noUnusedVariables: todo: remove this
static async login(data: IUserForLogin) {
// todo: implement login
const { password, ...user } = await Repository.user.getUserByEmail(
data.email,
);

if (!user) {
throw new UnauthorizedError('UnauthorizedError');
}

const isCorrectPassword = await comparePassword(data.password, password);

if (!isCorrectPassword) {
throw new UnauthorizedError('UnauthorizedError');
}

const jwt = await Jwt.createToken(user);
return jwt;
}

static async register(data: IUserForRegister) {
const { first_name, last_name, email, password } = data;

const user = await User.getUserByEmail(email);
const user = await Repository.user.getUserByEmail(email);

if (user) {
throw new BadRequestError('Select another email');
Expand All @@ -31,6 +45,11 @@ export default class Auth {

const id = await Repository.user.createUser(registeredUser);

return id;
const token = await this.login({
email: data.email,
password: data.password,
});

return { id, token };
}
}
6 changes: 3 additions & 3 deletions server/src/utils/jwt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import jwt from 'jsonwebtoken';
import { UnauthorizedError } from './errorHandler';

export class Jwt {
static createToken(payload: string) {
static createToken(payload: object): Promise<string> {
return new Promise((resolve, reject) => {
jwt.sign(payload, process.env.JWT_PRIVATE_KEY, (error, token) => {
jwt.sign(payload, process.env.JWT_PRIVATE_KEY, (error, token: string) => {
if (error) {
reject('error when creating token');
reject(error);
} else {
resolve(token);
}
Expand Down

0 comments on commit 9471930

Please sign in to comment.