-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Garv <[email protected]>, working user …
…authentication
- Loading branch information
1 parent
bc05018
commit 2f8fc16
Showing
16 changed files
with
994 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// TODO : remove this later once the values are being used | ||
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
import express, { Router } from 'express'; | ||
import { v4 as uuidv4 } from 'uuid'; | ||
import { generateAccessAndRefreshTokens } from '../../utils/jwt'; | ||
import { addRefreshTokenToWhiteList } from './auth.services'; | ||
import { findUserByEmail, createUserByEmailAndPassword } from '../users/users.services'; | ||
|
||
const app = express(); | ||
//@see https://expressjs.com/en/guide/routing.html | ||
const authRouter = Router(); | ||
|
||
// TODO : Test this route using CURL | ||
app.post('/register', async (req, res, next) => { | ||
try { | ||
// object destructuring to help retrieve | ||
// @see https://www.geeksforgeeks.org/how-to-post-json-data-using-curl/ | ||
// this is the data we provide during an API call | ||
const { email, password } = req.body; | ||
if (!email || !password) { | ||
res.status(400); | ||
throw new Error('You must provide an email and a password.'); | ||
} | ||
|
||
const existingUser = await findUserByEmail(email); | ||
|
||
if (existingUser) { | ||
res.status(404).send({ | ||
message: `The email ${email} is already in use, please login instead.`, | ||
}); | ||
throw new Error('The email is already in use'); | ||
} | ||
|
||
const user = await createUserByEmailAndPassword({ email, password }); | ||
// @see https://stackoverflow.com/questions/20342058/which-uuid-version-to-use | ||
const jti = uuidv4(); | ||
const { accessToken, refreshToken } = generateAccessAndRefreshTokens(user, jti); | ||
// TODO : remove this comment maybe | ||
// jti is the unique id assigned to the newly created user, think of it as the primary key | ||
await addRefreshTokenToWhiteList({ jti, refreshToken, userId: user.id }); | ||
/*res.status(200).send({ | ||
message: 'Successfully created new user!', | ||
}); */ | ||
|
||
res | ||
.json({ | ||
accessToken, | ||
refreshToken, | ||
}) | ||
.status(200); | ||
} catch (err) { | ||
//next(err); | ||
console.error(err); | ||
} | ||
}); | ||
|
||
//export default authRouter; | ||
/* | ||
app.use('/auth', authRouter); | ||
app.listen('4000', () => { | ||
try { | ||
console.log('Connection successful!'); | ||
} catch (err) { | ||
console.error(err); | ||
} | ||
}); | ||
*/ | ||
export { authRouter }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
|
||
POST http://localhost:4001/register | ||
content-type: application/json | ||
|
||
{ | ||
"email" :"[email protected]", | ||
"password":"123123asdasd" | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// ? for learning/refresher on how routes work, not relevant to project | ||
// ! Use this for dirty playground for testing/defining any preliminary api calls | ||
//import express from 'express'; | ||
|
||
// TODO : file for running and testing to better understand how express works as a whole | ||
/* | ||
const app = express(); | ||
// define some example routes | ||
// hello world for express | ||
// this | ||
// @param1 method or path for API call being made | ||
// @param2 handler function to determine what to do once the request is recieved and what kind of response logic should take place | ||
app.get('/testRoute', function (_req, res) { | ||
res.send('Successful api call'); | ||
// the following are various mthods | ||
// one mthod of using it is to retireve a file from the request and download it to local storage | ||
res.download('../auth/test.rest'); | ||
res.jsonp({ | ||
// it can be any message as needed | ||
message: 'File has been donwloaded', | ||
}); | ||
// end the response | ||
res.end(); | ||
}); | ||
app.get('/retrieveID', async (req, res) => { | ||
//@see https://stackoverflow.com/questions/17007997/how-to-access-the-get-parameters-after-in-express | ||
// explains how to retrieve query parameters made during an API call | ||
const queryParams = await req.query.id; | ||
res.send(`The query param is : ${queryParams}`); | ||
res.end(); | ||
}); | ||
// define where the app should be running, meaning define the custom port | ||
//@example call | ||
/** | ||
* curl -X GET "http://localhost:4000/retrieveID?id= | ||
10" | ||
* must be encased around string based values | ||
// ! understanding how the next() function, aka callback function, also known as middleware works in this scenario | ||
// next allow us to control api flow, and also allows us to overload and call on the same route more than once | ||
app.get('/testRouteWithCallback', function (_req, res, next) { | ||
res.write('This should execute\n'); | ||
next(); | ||
}); | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
app.get('/testRouteWithCallback', function (_req, res, next) { | ||
res.write('This should execute after'); | ||
// terminates the request | ||
next(); | ||
res.end(); | ||
}); | ||
app.listen(4000, () => { | ||
console.log('App listening to port 5000!'); | ||
}); | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
//! use this to add all the relevant main routes here | ||
|
||
//import { authRouter } from './auth/auth.routes'; | ||
import express from 'express'; | ||
|
||
const app = express(); | ||
// ** needed to add express.json() | ||
app.use(express.json()); | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
const router = express.Router(); | ||
//router.use('/auth', authRouter); | ||
//http://localhost:4001/auth/register | ||
|
||
app.get('/test', (req, res) => { | ||
console.log('hello world'); | ||
res.send('Hello from A!').status(200); | ||
}); | ||
|
||
app.listen('4001', () => { | ||
console.log(`Listening to port 4001`); | ||
}); | ||
|
||
import { v4 as uuidv4 } from 'uuid'; | ||
import { generateAccessAndRefreshTokens } from '../utils/jwt'; | ||
import { addRefreshTokenToWhiteList } from './auth/auth.services'; | ||
import { findUserByEmail, createUserByEmailAndPassword } from './users/users.services'; | ||
|
||
//const app = express(); | ||
//@see https://expressjs.com/en/guide/routing.html | ||
const authRouter = express.Router(); | ||
|
||
// TODO : Test this route using CURL | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
app.post('/register', async (req, res, next) => { | ||
try { | ||
// @see https://www.geeksforgeeks.org/how-to-post-json-data-using-curl/ | ||
// ! issue here | ||
if (req.body) { | ||
console.log('body', req.body); | ||
// console.log('lot', JSON.parse(req.body)); | ||
} else { | ||
console.log('Unable to extract data'); | ||
} | ||
|
||
//return; | ||
const { email, password } = req.body; | ||
if (!email || !password) { | ||
res.status(400); | ||
throw new Error('You must provide an email and a password.'); | ||
} | ||
|
||
const existingUser = await findUserByEmail(email); | ||
|
||
if (existingUser) { | ||
res.status(404).send({ | ||
message: `The email ${email} is already in use, please login instead.`, | ||
}); | ||
throw new Error('The email is already in use'); | ||
} | ||
|
||
const user = await createUserByEmailAndPassword({ email, password }); | ||
// @see https://stackoverflow.com/questions/20342058/which-uuid-version-to-use | ||
const jti = uuidv4(); | ||
const { accessToken, refreshToken } = generateAccessAndRefreshTokens(user, jti); | ||
// TODO : remove this comment maybe | ||
// jti is the unique id assigned to the newly created user, think of it as the primary key | ||
await addRefreshTokenToWhiteList({ jti, refreshToken, userId: user.id }); | ||
|
||
res | ||
.json({ | ||
accessToken, | ||
refreshToken, | ||
}) | ||
.status(200); | ||
} catch (err) { | ||
//next(err); | ||
console.error(err); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
import { PrismaClient } from '@prisma/client'; | ||
const db = new PrismaClient(); | ||
export default db; | ||
export const db = new PrismaClient(); |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.