Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New structure #3

Merged
merged 4 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
});
22 changes: 13 additions & 9 deletions apps/auth/src/routes.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,44 @@
import koaRouter from 'koa-router';
import crypto from 'crypto';
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 = crypto.randomBytes(2).toString('hex');
const email = ctx.request.body;
const otp = generateOTP(2);
const { email } = ctx.request.body;

ctx.app.user = { email: email.email, otp };
ctx.app.user = { email: email, otp }; //save temporally in memory

ctx.body = { otp };
await sendOTPEmail(email, otp);
ctx.body = { message: 'OTP sent to your email' };

next();
});

router.post('/otp/verify', async (ctx, next) => {
const { otp: userOtp, email } = ctx.request.body;
console.log('users', ctx.app.user);

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';
22 changes: 22 additions & 0 deletions apps/login-ui/src/app/AppRoutes.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import React from 'react';
import App from './App';
import { Route } from '@onboarding/router/data';
import { CustomRouter } from '@onboarding/router/feature';
import { OtpPageAuth } from '@onboarding/otp-ui/feature';

const routes: Route[] = [
{
path: '/',
element: <App />,
},
{
path: '/login',
element: <OtpPageAuth />,
},
];

const AppRoutes: React.FC = () => {
return <CustomRouter routes={routes} />;
};

export default AppRoutes;
10 changes: 8 additions & 2 deletions apps/login-ui/src/app/AppStarter.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import React from 'react';
import { ThemeProvider } from '@mui/material';
import { theme } from '@onboarding/themes';
import App from './App';
import { I18nProvider } from '@onboarding/i18n/feature';
import { ReactQueryProvider } from '@onboarding/react-query/feature';
import AppRoutes from './AppRoutes';
import { AuthenticationProvider } from '@onboarding/otp-ui/feature';

const translations = {
//cant import of external file. eslint error
Expand All @@ -22,7 +24,11 @@ export const AppStarter: React.FC<Props> = () => {
const children = (
<ThemeProvider theme={theme}>
<I18nProvider translations={translations} locale={locale}>
<App />
<ReactQueryProvider>
<AuthenticationProvider>
<AppRoutes />
</AuthenticationProvider>
</ReactQueryProvider>
</I18nProvider>
</ThemeProvider>
);
Expand Down
22 changes: 0 additions & 22 deletions apps/otp/index.html

This file was deleted.

10 changes: 0 additions & 10 deletions apps/otp/jest.config.ts

This file was deleted.

9 changes: 0 additions & 9 deletions apps/otp/project.json

This file was deleted.

Binary file removed apps/otp/public/favicon.ico
Binary file not shown.
Empty file removed apps/otp/src/app/app.module.css
Empty file.
15 changes: 0 additions & 15 deletions apps/otp/src/app/app.spec.tsx

This file was deleted.

This file was deleted.

Empty file removed apps/otp/src/assets/.gitkeep
Empty file.
26 changes: 0 additions & 26 deletions apps/otp/src/main.tsx

This file was deleted.

1 change: 0 additions & 1 deletion apps/otp/src/styles.css

This file was deleted.

2 changes: 1 addition & 1 deletion libs/i18n/data/project.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "data",
"name": "data-i18n",
"$schema": "../../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "libs/i18n/data/src",
"projectType": "library",
Expand Down
2 changes: 1 addition & 1 deletion libs/i18n/feature/project.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "feature",
"name": "feature-i18n",
"$schema": "../../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "libs/i18n/feature/src",
"projectType": "library",
Expand Down
7 changes: 0 additions & 7 deletions libs/i18n/ui/README.md

This file was deleted.

9 changes: 0 additions & 9 deletions libs/i18n/ui/project.json

This file was deleted.

1 change: 0 additions & 1 deletion libs/i18n/ui/src/index.ts

This file was deleted.

9 changes: 0 additions & 9 deletions libs/i18n/ui/src/lib/ui.tsx

This file was deleted.

20 changes: 20 additions & 0 deletions libs/otp-ui/feature/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"presets": [
[
"@nx/react/babel",
{
"runtime": "automatic",
"useBuiltIns": "usage"
}
]
],
"plugins": [
[
"styled-components",
{
"pure": true,
"ssr": true
}
]
]
}
7 changes: 7 additions & 0 deletions libs/otp-ui/feature/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# feature

This library was generated with [Nx](https://nx.dev).

## Running unit tests

Run `nx test feature` to execute the unit tests via [Vitest](https://vitest.dev/).
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const nx = require('@nx/eslint-plugin');
const baseConfig = require('../../eslint.config.js');
const baseConfig = require('../../../eslint.config.js');

module.exports = [
...baseConfig,
Expand Down
Loading
Loading