Skip to content
Snippets Groups Projects
Commit b4d5f557 authored by Everett, Josh (UG - Comp Sci & Elec Eng)'s avatar Everett, Josh (UG - Comp Sci & Elec Eng)
Browse files

registration start

parent f06dec45
No related branches found
No related tags found
No related merge requests found
# Auth Service
# Endpoints
## Login
```
/auth/login
```
Request format:
```json
{
"email": "joebloggs@gmail.com",
"password": "password123"
}
```
Responses:
## Register
```
/auth/register
```
# JWT Validation
How to validate the JWT and extract the user informtion from it
import { Request, Response } from "express"; import { Request, Response } from "express";
import Joi from "joi"; import Joi from "joi";
import { TokenService } from "./token_service"; import { ITokenPayload, TokenService } from "./token_service";
interface ILoginResponse {
token: string;
}
interface ILoginRequest { interface ILoginRequest {
email: string; email: string;
...@@ -16,7 +14,7 @@ export class LoginHandler { ...@@ -16,7 +14,7 @@ export class LoginHandler {
} }
public async handle(req: Request, res: Response): Promise<Response> { public async handle(req: Request, res: Response): Promise<Response> {
console.log(req.body);
const schema = Joi.object({ const schema = Joi.object({
email: Joi.string().email().required(), email: Joi.string().email().required(),
password: Joi.string().min(8).required() password: Joi.string().min(8).required()
...@@ -46,8 +44,11 @@ export class LoginHandler { ...@@ -46,8 +44,11 @@ export class LoginHandler {
} }
} }
private async login(req: ILoginRequest ): Promise<ILoginResponse | null>{ public async login(req: ILoginRequest ): Promise<ITokenPayload | null>{
// fetch from the database the following user object and if the user exists and the password hash matches the password hash in the database
// then return the token
// otherwise return null
const user : IUser = { const user : IUser = {
id: `id-${req.email}`, id: `id-${req.email}`,
email: req.email, email: req.email,
......
import Joi from "joi";
import { Request, Response } from "express";
import { ITokenPayload, TokenService } from "./token_service";
interface IRegistrationRequest {
email: string;
password: string;
fullName: string;
}
export class RegistrationHandler {
constructor(private tokenService: TokenService){}
public async handle(req: Request, res: Response): Promise<Response> {
const schema = Joi.object({
email: Joi.string().email().required(),
password: Joi.string().min(8).required(),
fullName: Joi.string().required()
})
try {
const {value, error } = schema.validate(req.body);
if(error) {
console.log('validation error: ', error.message);
return res.status(400).send({message: error!.message});
}
const {email, password, fullName} = value;
const responseBody = await this.register({email, password, fullName});
if(!responseBody) {
return res.status(403).send({message: 'There already exists a user with that email address'});
}
return res.status(200).send(responseBody);
}
catch (err) {
console.log('Error: ', err);
return res.status(500).send(err);
}
}
public async register(req: IRegistrationRequest ): Promise<ITokenPayload| null>{
// Check that there isn't already a user in the database with the same email address
// If there is then return null
// Otherwise create a new user in the database and return the token
const user : IUser = {
id: `id-${req.email}`,
email: req.email,
password: req.password,
fullName: req.fullName,
}
const token = await this.tokenService.generateToken(user);
return token;
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment