Skip to content
This repository has been archived by the owner on Dec 13, 2024. It is now read-only.

Commit

Permalink
Merge pull request #41 from p-society/core
Browse files Browse the repository at this point in the history
feat/fix(core): completes player crud and authentication, mail integrations
  • Loading branch information
zakhaev26 authored Mar 3, 2024
2 parents aec231c + dd2d4d6 commit 4cafe24
Show file tree
Hide file tree
Showing 26 changed files with 611 additions and 179 deletions.
198 changes: 197 additions & 1 deletion core/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# core

> core services for gc-server
> core service for gc server
## About

Expand Down Expand Up @@ -43,3 +43,199 @@ $ feathers help # Show all commands
## Help
For more information on all the things you can do with Feathers visit [docs.feathersjs.com](http://docs.feathersjs.com).
-----------------------------------------------------
1. Create a Player
Request :
POST http:127.0.0.1:3030/player
```
curl --location 'http://127.0.0.1:3030/player' \
--header 'Content-Type: application/json' \
--data-raw '{
"email":"[email protected]",
"firstName":"Soubhik",
"lastName":"Gon"1
"password":"b422056",
"role":32768,
"sport":"football",
"branch":"it",
"year":2,
"contactNo":"6370462354",
"socials":[{
"instagram":"soubhikgon_",
"linkedin":"soubhikgon"
}]
}'
```
Response:
```
{
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJwbGF5ZXIiOnsiZW1haWwiOiJiNDIyMDU2QGlpaXQtYmguYWMuaW4iLCJmaXJzdE5hbWUiOiJTb3ViaGlrIiwibGFzdE5hbWUiOiJHb24iLCJwYXNzd29yZCI6IiQyYSQxMCRoQjh4VzczYllKY0thSmV6Um9qem4uRk42bkxPMHZWZXJZNG1SVTlOU25UQzEub1VyLlBwVyIsInJvbGUiOjEsInNwb3J0IjoiZm9vdGJhbGwiLCJicmFuY2giOiJpdCIsInllYXIiOjIsImNvbnRhY3RObyI6IjYzNzA0NjIzNTQiLCJzb2NpYWxzIjpbeyJpbnN0YWdyYW0iOiJzb3ViaGlrZ29uXyIsImxpbmtlZGluIjoic291Ymhpa2dvbiJ9XX0sIm90cCI6Nzk2MzIsImlhdCI6MTcwOTQ2NzU1Nn0.PJW_PJnrrbGM1bbKfac7qWGXHHn5N0Mnw79zFG7ZtUA"
}
```
2. Verification Player
Request :
POST http://127.0.0.1:3030/player/verification
```
curl --location 'http://127.0.0.1:3030/player/verification' \
--header 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJwbGF5ZXIiOnsiZW1haWwiOiJiNDIyMDU2QGlpaXQtYmguYWMuaW4iLCJmaXJzdE5hbWUiOiJTb3ViaGlrIiwibGFzdE5hbWUiOiJHb24iLCJwYXNzd29yZCI6IiQyYSQxMCRoQjh4VzczYllKY0thSmV6Um9qem4uRk42bkxPMHZWZXJZNG1SVTlOU25UQzEub1VyLlBwVyIsInJvbGUiOjEsInNwb3J0IjoiZm9vdGJhbGwiLCJicmFuY2giOiJpdCIsInllYXIiOjIsImNvbnRhY3RObyI6IjYzNzA0NjIzNTQiLCJzb2NpYWxzIjpbeyJpbnN0YWdyYW0iOiJzb3ViaGlrZ29uXyIsImxpbmtlZGluIjoic291Ymhpa2dvbiJ9XX0sIm90cCI6Nzk2MzIsImlhdCI6MTcwOTQ2NzU1Nn0.PJW_PJnrrbGM1bbKfac7qWGXHHn5N0Mnw79zFG7ZtUA' \
--header 'Content-Type: application/json' \
--data '{
"otp":79632
}'
```
Response :
```
{
"firstName": "Soubhik",
"lastName": "Gon",
"email": "[email protected]",
"password": "$2a$10$hB8xW73bYJcKaJezRojzn.FN6nLO0vVerY4mRU9NSnTC1.oUr.PpW",
"role": "1",
"sport": "football",
"branch": "it",
"year": 2,
"contactNo": "6370462354",
"socials": [
{
"instagram": "soubhikgon_",
"linkedin": "soubhikgon"
}
],
"isDeleted": false,
"_id": "65e46a9ff6c215083c5b4afc",
"createdAt": "2024-03-03T12:18:39.317Z",
"updatedAt": "2024-03-03T12:18:39.317Z",
"__v": 0
}
```
3. Login
Request :
POST http://127.0.0.1:3030/authenticate
```
curl --location 'http://127.0.0.1:3030/authentication' \
--header 'Content-Type: application/json' \
--data-raw '{
"strategy":"local",
"email":"[email protected]",
"password":"b422056"
}'
```
Response :
<details>
<summary>Click to view response </summary>
```
{
"accessToken": "eyJhbGciOiJIUzI1NiIsInR5cCI6ImFjY2VzcyJ9.eyJpYXQiOjE3MDk0Njg0NzgsImV4cCI6MTc0MTAwNDQ3OCwiYXVkIjoiaHR0cHM6Ly95b3VyZG9tYWluLmNvbSIsImlzcyI6ImZlYXRoZXJzIiwic3ViIjoiNjVlNDZhOWZmNmMyMTUwODNjNWI0YWZjIiwianRpIjoiYjNiZmE0NjktNjdiMS00ZWNmLTkyZWItNTMwZWI0OWVkZjFlIn0.ji7tjDmk-e99XZxy6N77FpOok2QNGBsZfoI-IiMskME",
"authentication": {
"strategy": "local",
"accessToken": "eyJhbGciOiJIUzI1NiIsInR5cCI6ImFjY2VzcyJ9.eyJpYXQiOjE3MDk0Njg0NzgsImV4cCI6MTc0MTAwNDQ3OCwiYXVkIjoiaHR0cHM6Ly95b3VyZG9tYWluLmNvbSIsImlzcyI6ImZlYXRoZXJzIiwic3ViIjoiNjVlNDZhOWZmNmMyMTUwODNjNWI0YWZjIiwianRpIjoiYjNiZmE0NjktNjdiMS00ZWNmLTkyZWItNTMwZWI0OWVkZjFlIn0.ji7tjDmk-e99XZxy6N77FpOok2QNGBsZfoI-IiMskME",
"payload": {
"iat": 1709468478,
"exp": 1741004478,
"aud": "https://yourdomain.com",
"iss": "feathers",
"sub": "65e46a9ff6c215083c5b4afc",
"jti": "b3bfa469-67b1-4ecf-92eb-530eb49edf1e"
}
},
"player": {
"_id": "65e46a9ff6c215083c5b4afc",
"firstName": "Soubhik",
"lastName": "Gon",
"email": "[email protected]",
"role": "1",
"sport": "football",
"branch": "it",
"year": 2,
"contactNo": "6370462354",
"socials": [
{
"instagram": "soubhikgon_",
"linkedin": "soubhikgon"
}
],
"isDeleted": false,
"createdAt": "2024-03-03T12:18:39.317Z",
"updatedAt": "2024-03-03T12:18:39.317Z",
"__v": 0
}
}
```
</details>
4. Patch a Player
Request :
PATCH http://127.0.0.1:3030/player
```
curl --location --request PATCH 'http://127.0.0.1:3030/player/65e46a9ff6c215083c5b4afc' \
--header 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6ImFjY2VzcyJ9.eyJpYXQiOjE3MDk0Njg3NTcsImV4cCI6MTc0MTAwNDc1NywiYXVkIjoiaHR0cHM6Ly95b3VyZG9tYWluLmNvbSIsImlzcyI6ImZlYXRoZXJzIiwic3ViIjoiNjVlNDZhOWZmNmMyMTUwODNjNWI0YWZjIiwianRpIjoiMGMxNjBhYTItZGM0OC00ZjdjLWEwYmEtYWU3OWQxNTQ2NTY4In0.hwgaTxyfROYHUuWdn4rsTYpfcdq0sfq82iqipqy_RbY' \
--header 'Content-Type: application/json' \
--data '{
"firstName":"Test"
}'
```
```
{
"_id": "65e46a9ff6c215083c5b4afc",
"firstName": "Test",
"lastName": "Gon",
"email": "[email protected]",
"role": "1",
"sport": "football",
"branch": "it",
"year": 2,
"contactNo": "6370462354",
"socials": [
{
"instagram": "soubhikgon_",
"linkedin": "soubhikgon"
}
],
"isDeleted": false,
"createdAt": "2024-03-03T12:18:39.317Z",
"updatedAt": "2024-03-03T12:26:33.962Z",
"__v": 0
}
```
5. Delete a Player
Request:
DELETE http://127.0.0.1:3030/65e46e31f966475b12fed6b0
```
curl --location --request DELETE 'http://127.0.0.1:3030/player/65e46e31f966475b12fed6b0' \
--header 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6ImFjY2VzcyJ9.eyJpYXQiOjE3MDk0NjkzMDMsImV4cCI6MTc0MTAwNTMwMywiYXVkIjoiaHR0cHM6Ly95b3VyZG9tYWluLmNvbSIsImlzcyI6ImZlYXRoZXJzIiwic3ViIjoiNjVlNDZlMzFmOTY2NDc1YjEyZmVkNmIwIiwianRpIjoiMDA2ZTdhZWMtMDFiNS00MGFmLTg0Y2QtMGM3MTNhMGNkYTk5In0.nPp332b4H0_iswyT0xrL899Q6eqZHnJGElsIbb7Dlf0' \
--data ''
```
Response:
TBD (deleted tho)
6 changes: 3 additions & 3 deletions core/config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
},
"authentication": {
"entity": "player",
"service": "players",
"secret": "AolPT71hD5ynh/z2frT7qdFo6Cw=",
"service": "player",
"secret": "cgDSGvUCeTqq33rFMjfo6ezFVMw=",
"authStrategies": [
"jwt",
"local"
Expand All @@ -21,7 +21,7 @@
"audience": "https://yourdomain.com",
"issuer": "feathers",
"algorithm": "HS256",
"expiresIn": "1d"
"expiresIn": "365d"
},
"local": {
"usernameField": "email",
Expand Down
5 changes: 2 additions & 3 deletions core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "core",
"description": "core services for gc-server",
"description": "core service for gc server",
"version": "0.0.0",
"homepage": "",
"private": true,
Expand Down Expand Up @@ -48,10 +48,9 @@
"@feathersjs/feathers": "^4.5.15",
"@feathersjs/socketio": "^4.5.15",
"@feathersjs/transport-commons": "^5.0.23",
"@types/express": "^4.17.21",
"@types/node": "^20.11.24",
"compression": "^1.7.4",
"cors": "^2.8.5",
"feathers-hooks-common": "^8.1.2",
"feathers-mongoose": "^8.5.1",
"helmet": "^5.1.0",
"mongodb-core": "^3.2.7",
Expand Down
62 changes: 1 addition & 61 deletions core/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import configuration from '@feathersjs/configuration';
import express from '@feathersjs/express';
import socketio from '@feathersjs/socketio';

import { Request } from "express"

import { Application } from './declarations';
import logger from './logger';
import middleware from './middleware';
Expand All @@ -19,12 +19,6 @@ import channels from './channels';
import { HookContext as FeathersHookContext } from '@feathersjs/feathers';
import authentication from './authentication';
import mongoose from './mongoose';
import { generateJWT } from './middleware/generateJwt';
import { decodeJWT } from './middleware/decodeJwt';
import { extractTokenFromHeader } from './utils/extractTokenFromHeader';
import sendMail from './utils/sendMail';
import generateOtp from './utils/generateOtp';
import { Service } from 'feathers-mongoose';
// Don't remove this comment. It's needed to format import lines nicely.

const app: Application = express(feathers());
Expand Down Expand Up @@ -54,61 +48,7 @@ app.configure(mongoose);
app.configure(middleware);
app.configure(authentication);
// Set up our services (see `services/index.ts`)

// @ts-ignore
app.post("/players", async (req, res) => {
try {
const player = req.body;
const OTP = generateOtp();

await sendMail(player.email, OTP);

const verificationToken = generateJWT(player, app, OTP);

res.json({ verificationToken });
} catch (error) {
console.error("Error:", error);
res.status(500).json({ error: "An error occurred while processing your request" });
}
});

app.post("/players/callback/verification", async (req: Request, res: Response): Promise<any> => {
try {
const { otp: typedOtp } = req.body;
const token = extractTokenFromHeader(req)
if (!token) {
// @ts-ignore
return res.status(401).json({ error: 'Unauthorized: Missing token' });
}

const { player ,otp} = decodeJWT(token, app)
console.log("player = ", player)
if (otp === typedOtp) {
const PlayerService: Service = app.service('players')

const playerData = new PlayerService.Model({
email: player.email,
password: player.password,
})
const savedPlayer = await playerData.save()
console.log("correct")
// @ts-ignore
return res.status(200).json({ ...savedPlayer });
} else {
console.log("Invalid OTP")
// @ts-ignore
return res.status(400).json({ error: 'Invalid OTP' });
}
} catch (e: any) {
console.error('Error:', e.message);
// @ts-ignore
return res.status(500).json({ error: 'Internal server error' });
}
})


app.configure(services);

// Set up event channels (see channels.ts)
app.configure(channels);

Expand Down
17 changes: 17 additions & 0 deletions core/src/constants/branch.enum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
enum BranchEnum {
CSE = 'cse',
IT = 'it',
CE = 'ce',
ETC = 'etc',
EEE = 'eee',
}

export const BranchEnumList = [
BranchEnum.CE,
BranchEnum.CSE,
BranchEnum.ETC,
BranchEnum.EEE,
BranchEnum.IT,
];

export default BranchEnum;
13 changes: 13 additions & 0 deletions core/src/constants/roles.enum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
enum RolesEnum {
PLAYER = 1,
ADMIN = 512,
PLATFORM_SUPER_ADMIN = 32768,
}

export const RolesEnumList = [
RolesEnum.PLAYER,
RolesEnum.ADMIN,
RolesEnum.PLATFORM_SUPER_ADMIN,
];

export default RolesEnum;
21 changes: 21 additions & 0 deletions core/src/constants/sport.enum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
enum SportEnum {
CRICKET = 'cricket',
FOOTBALL = 'football',
BASKETBALL = 'basketball',
CHESS = 'chess',
VOLLEYBALL = 'volleyball',
BADMINTON = 'badminton',
CARROM = 'carrom',
}

export const SportEnumList = [
SportEnum.CRICKET,
SportEnum.FOOTBALL,
SportEnum.BASKETBALL,
SportEnum.CHESS,
SportEnum.VOLLEYBALL,
SportEnum.BADMINTON,
SportEnum.CARROM,
];

export default SportEnum;
Loading

0 comments on commit 4cafe24

Please sign in to comment.