Skip to content

Commit

Permalink
feat: jwt authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
javiruiz27 committed Dec 18, 2024
1 parent 1999934 commit 365d68d
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 37 deletions.
28 changes: 11 additions & 17 deletions apps/auth/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,23 @@
import koa from 'koa';
import router from "./routes";
import router from './routes';
import bodyParser from 'koa-bodyparser';
import cors from "koa-cors"
import jwtMiddleware from 'koa-jwt';

import cors from 'koa-cors';

const host = process.env.HOST ?? 'localhost';
const port = process.env.PORT ? Number(process.env.PORT) : 8080;
const secretKey = 'your-secret-key';

const app = new koa();
app.use(cors({
origin: '*',
credentials: true,
headers: ['Content-Type', 'Authorization'],
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS']
}));
app.use(jwtMiddleware({
secret: secretKey,
passthrough: true,
}));
app.use(bodyParser())
app.use(
cors({
origin: '*',
credentials: true,
headers: ['Content-Type', 'Authorization'],
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
})
);
app.use(bodyParser());
app.use(router.routes()).use(router.allowedMethods());

app.listen(port, host, () => {
console.log(`[ ready ] http://${host}:${port}`);
});

1 change: 1 addition & 0 deletions apps/auth/src/middlewares/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './jwt';
6 changes: 6 additions & 0 deletions apps/auth/src/middlewares/jwt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import koaJwt from 'koa-jwt';
import { jwtSecret } from '../utils';

export const jwtVal = koaJwt({
secret: jwtSecret, // Should not be hardcoded
});
9 changes: 7 additions & 2 deletions apps/auth/src/routes.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import koaRouter from 'koa-router';
import jwt from 'jsonwebtoken';
import { generateOTP, sendOTPEmail } from '@onboarding/otp/feature';
import { jwtSecret } from './utils';
import { jwtVal } from './middlewares/index';

const router = new koaRouter();

router.get('hello', '/', (ctx) => {
ctx.body = '<h1>Hello</h1>';
});

router.get('/data', jwtVal, async (ctx) => {
ctx.body = '<h1>hh</h1>';
});

router.post('/otp/generate', async (ctx, next) => {
const otp = generateOTP(2);
const { email } = ctx.request.body;
Expand All @@ -25,15 +31,14 @@ router.post('/otp/verify', async (ctx, next) => {
const storedOtp = ctx.app.user?.otp ?? ''; // retrieve stored OTP

if (userOtp === storedOtp) {
const token = jwt.sign({ user: email }, 'your-secret-key', {
const token = jwt.sign({ user: email }, jwtSecret, {
expiresIn: '1h',
});
ctx.body = { token };
} else {
ctx.status = 401;
ctx.body = { error: 'Invalid OTP' };
}

next();
});

Expand Down
1 change: 1 addition & 0 deletions apps/auth/src/utils/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const jwtSecret = 'n6dHKHspDp';
1 change: 1 addition & 0 deletions apps/auth/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './config';
36 changes: 18 additions & 18 deletions libs/otp/feature/src/nodemailer.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { createTransport } from "nodemailer";
import { createTransport } from 'nodemailer';

export const sendOTPEmail = async (email, otp) => {
const transporter = createTransport({
service: 'gmail',
auth: {
user: '[email protected]',
pass: 'tlrq fspt ecvu rcmi',
},
});
const mailOptions = {
from: '[email protected]',
to: email,
subject: 'Your OTP Code',
text: `Your OTP code is ${otp}`,
};
await transporter.sendMail(mailOptions);
};
const transporter = createTransport({
service: 'gmail',
auth: {
user: '[email protected]',
pass: '',
},
});

const mailOptions = {
from: '[email protected]',
to: email,
subject: 'Your OTP Code',
text: `Your OTP code is ${otp}`,
};

await transporter.sendMail(mailOptions);
};

0 comments on commit 365d68d

Please sign in to comment.