Register and login and assign jwt token
This commit is contained in:
23
api/src/db/usersSchema.ts
Normal file
23
api/src/db/usersSchema.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { int, mysqlTable, text, bigint, varchar, double } from 'drizzle-orm/mysql-core';
|
||||
import { createInsertSchema , createSelectSchema, createUpdateSchema } from 'drizzle-zod';
|
||||
|
||||
export const usersTable = mysqlTable('users', {
|
||||
id: int().autoincrement().primaryKey(),
|
||||
email: varchar({ length: 255 }).notNull().unique(),
|
||||
password: varchar({ length: 255 }).notNull(),
|
||||
role: varchar({ length: 255 }).notNull().default('user'),
|
||||
|
||||
name: varchar({ length: 255 }).notNull(),
|
||||
address: text(),
|
||||
|
||||
});
|
||||
|
||||
export const createUserSchema = createInsertSchema(usersTable).omit({
|
||||
id: true,
|
||||
role: true,
|
||||
});
|
||||
|
||||
export const loginSchema = createSelectSchema(usersTable).pick({
|
||||
email: true,
|
||||
password: true,
|
||||
});
|
||||
@@ -1,5 +1,6 @@
|
||||
import express, {json, urlencoded} from 'express';
|
||||
import productsRoutes from './routes/products/index'
|
||||
import authRoutes from './routes/auth/index'
|
||||
|
||||
const port = 3000;
|
||||
const app = express();
|
||||
@@ -15,6 +16,7 @@ app.get('/', (req, res) => {
|
||||
|
||||
|
||||
app.use ('/products', productsRoutes);
|
||||
app.use ('/auth', authRoutes);
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log(`Example app listening on port ${port}`);
|
||||
|
||||
72
api/src/routes/auth/index.ts
Normal file
72
api/src/routes/auth/index.ts
Normal file
@@ -0,0 +1,72 @@
|
||||
import { createUserSchema, loginSchema, usersTable } from "../../db/usersSchema";
|
||||
import { validateData } from "../../middlewares/validationMiddleware";
|
||||
import { Router } from "express";
|
||||
import {db} from '../../db/index';
|
||||
import {eq} from 'drizzle-orm';
|
||||
import bcrypt from 'bcryptjs';
|
||||
import jwt from 'jsonwebtoken'
|
||||
|
||||
const router = Router();
|
||||
|
||||
router.post ("/register", validateData(createUserSchema), async (req, res) => {
|
||||
try{
|
||||
console.log (req.cleanBody);
|
||||
const data = req.cleanBody;
|
||||
data.password = await bcrypt.hash(data.password, 10);
|
||||
|
||||
const userId = await db
|
||||
.insert(usersTable)
|
||||
.values(data)
|
||||
.$returningId();
|
||||
|
||||
// const [user] = await db
|
||||
// .select()
|
||||
// .from(usersTable)
|
||||
// .where(eq(usersTable.id, userId));
|
||||
|
||||
res.status(201).json({userId});
|
||||
return;
|
||||
}catch(e){
|
||||
res.status(500);
|
||||
return;
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
router.post ("/login", validateData(loginSchema) , async (req, res) => {
|
||||
try{
|
||||
const {email, password} = req.cleanBody;
|
||||
console.log ({email, password});
|
||||
const [user] = await db
|
||||
.select()
|
||||
.from(usersTable)
|
||||
.where(eq(usersTable.email, email));
|
||||
console.log(user);
|
||||
if (!user){
|
||||
res.status(401).json({error: "Authentication error"});
|
||||
return;
|
||||
}
|
||||
|
||||
const matched = await bcrypt.compare(password, user.password);
|
||||
console.log(matched);
|
||||
if (!matched){
|
||||
res.status(401).json({error: "Authentication error"});
|
||||
return;
|
||||
}
|
||||
|
||||
const token = jwt.sign(
|
||||
{userId: user.id, role: user.role},
|
||||
'your-secret',
|
||||
{expiresIn: '12h'}
|
||||
);
|
||||
//@ts-ignore
|
||||
delete user.password;
|
||||
res.status(200).json({token, user});
|
||||
}catch(e){
|
||||
res.status(500);
|
||||
return;
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
export default router;
|
||||
Reference in New Issue
Block a user