feat(auth): auth config init
This commit is contained in:
5
back-express/src/routes/auth/auth.routes.ts
Normal file
5
back-express/src/routes/auth/auth.routes.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import { Router } from "express";
|
||||
|
||||
export const authRoutes = Router();
|
||||
|
||||
authRoutes.get("/signIn", async (req, res) => {});
|
||||
37
back-express/src/routes/auth/auth.service.ts
Normal file
37
back-express/src/routes/auth/auth.service.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import { sign, verify } from "jsonwebtoken";
|
||||
import UserService from "../users/users.service";
|
||||
|
||||
const secret_key = "asdf";
|
||||
|
||||
export class AuthService {
|
||||
constructor(private usersService: UserService) {}
|
||||
|
||||
async signIn(username: string, password: string) {
|
||||
const user = await this.usersService.getUserByUsername(username);
|
||||
|
||||
if (!user) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const payload = {
|
||||
sub: user.id,
|
||||
username: user.username,
|
||||
roles: user.roles,
|
||||
picture: user.picture,
|
||||
};
|
||||
|
||||
const token = sign(payload, secret_key, { expiresIn: "1h" });
|
||||
return token;
|
||||
}
|
||||
|
||||
async verifyToken(jwt: string) {
|
||||
const token = jwt.split(".")[1];
|
||||
if (!token) return false;
|
||||
try {
|
||||
const payload = verify(token, secret_key);
|
||||
return payload.username;
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
0
back-express/src/routes/auth/auth.types.ts
Normal file
0
back-express/src/routes/auth/auth.types.ts
Normal file
@@ -3,9 +3,11 @@ import express from "express";
|
||||
import { defaultRoute } from "./default.route";
|
||||
import { exampleRoutes } from "./example/example.routes";
|
||||
import { userRoutes } from "./users/users.routes";
|
||||
import { authRoutes } from "./auth/auth.routes";
|
||||
|
||||
export const routes = express.Router();
|
||||
|
||||
routes.use("/", defaultRoute);
|
||||
routes.use("/example", exampleRoutes);
|
||||
routes.use("/users", userRoutes);
|
||||
routes.use("/auth", authRoutes);
|
||||
|
||||
Reference in New Issue
Block a user