Skip to content

Commit

Permalink
chore: updated login controller
Browse files Browse the repository at this point in the history
  • Loading branch information
incredible-phoenix246 committed Jul 20, 2024
1 parent 1d37526 commit 55db569
Show file tree
Hide file tree
Showing 13 changed files with 83 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/controllers/UserController.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// src/controllers/UserController.ts
import { Request, Response } from "express";
import { UserService } from "../services/UserServivce";
import { UserService } from "../services/user.services";

class UserController {
private userService: UserService;
Expand Down
8 changes: 3 additions & 5 deletions src/models/article.ts → src/models/helpcentertopic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ import {
Column,
CreateDateColumn,
UpdateDateColumn,
ManyToOne,
} from "typeorm";
import ExtendedBaseEntity from "./extended-base-entity";
import { User } from ".";

@Entity()
export class Article extends ExtendedBaseEntity {
export class HelpCenterTopic extends ExtendedBaseEntity {
@PrimaryGeneratedColumn("uuid")
id: string;

Expand All @@ -20,8 +18,8 @@ export class Article extends ExtendedBaseEntity {
@Column()
content: string;

@ManyToOne(() => User, (user) => user.articles)
author: User;
@Column()
author: string;

@CreateDateColumn()
createdAt: Date;
Expand Down
11 changes: 6 additions & 5 deletions src/models/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from "./User";
export * from "./Organization";
export * from "./Profile";
export * from "./Product";
export * from "./article";
export * from "./user";
export * from "./organization";
export * from "./profile";
export * from "./product";
export * from "./helpcentertopic";
export * from "./notification";
21 changes: 21 additions & 0 deletions src/models/notification.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Entity, PrimaryGeneratedColumn, Column, Unique } from "typeorm";
import ExtendedBaseEntity from "./extended-base-entity";

@Entity()
@Unique(["user_id"])
export class NotificationSetting extends ExtendedBaseEntity {
@PrimaryGeneratedColumn()
id: number;

@Column()
user_id: string;

@Column()
email_notifications: boolean;

@Column()
push_notifications: boolean;

@Column()
sms_notifications: boolean;
}
2 changes: 1 addition & 1 deletion src/models/Organization.ts → src/models/organization.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Entity, PrimaryGeneratedColumn, Column, ManyToMany } from "typeorm";
import { User } from "./User";
import { User } from "./user";
import ExtendedBaseEntity from "./extended-base-entity";

@Entity()
Expand Down
2 changes: 1 addition & 1 deletion src/models/Product.ts → src/models/product.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Entity, PrimaryGeneratedColumn, Column, ManyToOne } from "typeorm";
import { User } from "./User";
import { User } from "./user";
import ExtendedBaseEntity from "./extended-base-entity";

@Entity()
Expand Down
2 changes: 1 addition & 1 deletion src/models/Profile.ts → src/models/profile.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Entity, PrimaryGeneratedColumn, Column, OneToOne } from "typeorm";
import { User } from "./User";
import { User } from "./user";
import ExtendedBaseEntity from "./extended-base-entity";

@Entity()
Expand Down
28 changes: 28 additions & 0 deletions src/models/sms.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {
Entity,
PrimaryGeneratedColumn,
Column,
CreateDateColumn,
ManyToOne,
} from "typeorm";
import ExtendedBaseEntity from "./extended-base-entity";
import { User } from ".";

@Entity()
export class Sms extends ExtendedBaseEntity {
@PrimaryGeneratedColumn("uuid")
id: string;

@Column()
phone_number: string;

@Column()
message: string;

@Column()
@ManyToOne(() => User, (user) => user.id)
sender_id: User;

@CreateDateColumn()
createdAt: Date;
}
5 changes: 1 addition & 4 deletions src/models/User.ts → src/models/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
CreateDateColumn,
UpdateDateColumn,
} from "typeorm";
import { Profile, Product, Organization, Article } from ".";
import { Profile, Product, Organization } from ".";
import { IsEmail } from "class-validator";
import ExtendedBaseEntity from "./extended-base-entity";
import { getIsInvalidMessage } from "../utils";
Expand Down Expand Up @@ -59,9 +59,6 @@ export class User extends ExtendedBaseEntity {
@JoinTable()
products: Product[];

@OneToMany(() => Article, (article) => article.author, { cascade: true })
articles: Article[];

@ManyToMany(() => Organization, (organization) => organization.users, {
cascade: true,
})
Expand Down
14 changes: 11 additions & 3 deletions src/services/auth.services.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AppDataSource } from "../data-source";
import { User } from "../models";
import { Profile, User } from "../models";
import { IAuthService, IUserSignUp, IUserLogin } from "../types";
import { Conflict, HttpError } from "../middleware";
import { hashPassword, generateNumericOTP, comparePassword } from "../utils";
Expand Down Expand Up @@ -92,7 +92,9 @@ export class AuthService implements IAuthService {
}
}

public async login(payload: IUserLogin): Promise<{ access_token: string; user: Partial<User> }> {
public async login(
payload: IUserLogin
): Promise<{ access_token: string; user: Partial<User> }> {
const { email, password } = payload;

try {
Expand All @@ -107,7 +109,13 @@ export class AuthService implements IAuthService {
throw new HttpError(401, "Invalid credentials");
}

const access_token = jwt.sign({ userId: user.id }, config.TOKEN_SECRET, { expiresIn: "1d" });
if (!user.isverified) {
throw new HttpError(403, "Email not verified");
}

const access_token = jwt.sign({ userId: user.id }, config.TOKEN_SECRET, {
expiresIn: "1d",
});

const { password: _, ...userWithoutPassword } = user;

Expand Down
2 changes: 1 addition & 1 deletion src/services/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from "./auth.services";
export * from "./UserServivce";
export * from "./user.services";
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// src/services/UserService.ts
import { User } from "../models/User";
import { User } from "../models/user";
import { IUserService } from "../types";

export class UserService implements IUserService {
Expand Down
9 changes: 7 additions & 2 deletions src/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,21 @@ export interface IRole {
role: "super_admin" | "admin" | "user";
}

interface IUserSignUp {
export interface IUserSignUp {
firstName: string;
lastName: string;
email: string;
password: string;
phone: string;
}

export interface IUserLogin {
email: string;
password: string;
}

export interface IAuthService {
// login(email: string, password: string): Promise<User>;
login(payload: IUserLogin): Promise<unknown>;
signUp(payload: IUserSignUp, res: unknown): Promise<unknown>;
verifyEmail(token: string, otp: number): Promise<{ message: string }>;
}

0 comments on commit 55db569

Please sign in to comment.