Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 53472d2

Browse filesBrowse files
Merge pull request #27 from app-generator/step4/support-for-react
Added support for frontend
2 parents b6e7b59 + fb22592 commit 53472d2
Copy full SHA for 53472d2

File tree

8 files changed

+83
-23
lines changed
Filter options

8 files changed

+83
-23
lines changed

‎.env.sample

Copy file name to clipboard
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
PORT=5000
2+
GITHUB_OAUTH_CLIENT_ID=
3+
GITHUB_OAUTH_CLIENT_SECRET=
4+
GITHUB_OAUTH_REDIRECT_URL=
5+
SQLITE_PATH=./database.db
6+
SECRET=

‎GITHUB_AUTH.md

Copy file name to clipboard
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Github Authorization
2+
Follow the below steps to implement GitHub OAuth in your nodejs app
3+
4+
- Create OAuth App on github and get client_id and client_secret. Also specify OAuth callback url in OAuth App
5+
6+
- Save client_id, client_secret and callback url values in .env file (get help from .env.sample file)
7+
8+
- Create a frontend part (using template engine or any other library) to enable click to precede github OAuth
9+
10+
- Also save the secrets on frontend part
11+
12+
- On clicking github OAuth button it would redirect to github for authorization
13+
14+
- On successful authorization it would redirect back to callback url

‎README.md

Copy file name to clipboardExpand all lines: README.md
+15
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,21 @@ authorization: JWT_TOKEN (returned by Login request)
203203

204204
<br />
205205

206+
## ✨ Update role for existing user
207+
208+
Using npm:
209+
210+
```npm run update-role [user_id] [role_id (optional)]```
211+
212+
Using yarn:
213+
214+
```npm run update-role [user_id] [role_id (optional)]```
215+
216+
- [user_id] is the id of existing user to update role for.
217+
- [role_id] is the id of role: 1 for admin & 2 for user. If you don't provide any role_id it would update user to admin role.
218+
219+
<br />
220+
206221
## ✨ Run the Tests
207222

208223
```yarn test```

‎package.json

Copy file name to clipboardExpand all lines: package.json
+2-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"lint": "eslint src --ext .ts",
1818
"build": "tsc -p tsconfig.build.json",
1919
"typecheck": "tsc --noEmit",
20-
"typeorm": "node --require ts-node/register ./node_modules/typeorm/cli.js"
20+
"typeorm": "node --require ts-node/register ./node_modules/typeorm/cli.js",
21+
"update-role": "ts-node-dev src/update-role.ts"
2122
},
2223
"dependencies": {
2324
"axios": "^1.2.0",

‎src/controllers/auth.controller.ts

Copy file name to clipboardExpand all lines: src/controllers/auth.controller.ts
+3-12
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,15 @@ import {
55
} from '../services/session.service';
66
import { createUserWithToken } from '../services/user.service';
77

8-
const frontend_url = process.env.FRONTEND_URL ?? '/'
9-
108
export const githubOauthHandler = async (
119
req: Request,
1210
res: Response,
1311
) => {
1412
try {
1513
const code = req.query.code as string;
1614

17-
if (req.query.error) {
18-
return res.redirect(`${frontend_url}/login`);
19-
}
20-
2115
if (!code) {
22-
console.log('authorization code not provided')
16+
return res.json({error: 'authorization code not provided'})
2317
}
2418

2519
// Get access_token using code
@@ -29,11 +23,8 @@ export const githubOauthHandler = async (
2923
const userData = await getGithubUser({access_token});
3024

3125
const returnedUser = await createUserWithToken(userData)
32-
if(returnedUser) {
33-
res.redirect(frontend_url);
34-
}else {
35-
res.json({error: 'no user returned'})
36-
}
26+
27+
res.json({user: returnedUser})
3728

3829
} catch (err: any) {
3930
res.json({'error': err.message})

‎src/controllers/logout.controller.ts

Copy file name to clipboard
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import ActiveSession from '../models/activeSession';
2+
import { connection } from '../server/database';
3+
4+
export const logoutUser = (req: any, res: any) => {
5+
const { token } = req.body;
6+
const activeSessionRepository = connection!.getRepository(ActiveSession);
7+
8+
activeSessionRepository.delete({ token })
9+
.then(() => res.json({ success: true }))
10+
.catch(() => {
11+
res.json({ success: false, msg: 'Token revoked' });
12+
});
13+
}

‎src/routes/users.ts

Copy file name to clipboardExpand all lines: src/routes/users.ts
+2-10
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { checkToken } from '../config/safeRoutes';
1212
import ActiveSession from '../models/activeSession';
1313
import User from '../models/user';
1414
import { connection } from '../server/database';
15+
import { logoutUser } from '../controllers/logout.controller';
1516

1617
// eslint-disable-next-line new-cap
1718
const router = express.Router();
@@ -115,16 +116,7 @@ router.post('/login', (req, res) => {
115116
});
116117
});
117118

118-
router.post('/logout', checkToken, (req, res) => {
119-
const { token } = req.body;
120-
const activeSessionRepository = connection!.getRepository(ActiveSession);
121-
122-
activeSessionRepository.delete({ token })
123-
.then(() => res.json({ success: true }))
124-
.catch(() => {
125-
res.json({ success: false, msg: 'Token revoked' });
126-
});
127-
});
119+
router.post('/logout', checkToken, logoutUser);
128120

129121
router.post('/checkSession', checkToken, (_req, res) => {
130122
res.json({ success: true });

‎src/update-role.ts

Copy file name to clipboard
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import "dotenv/config";
2+
import User from "./models/user";
3+
import { connection, connect } from "./server/database";
4+
const userId = process.argv[2];
5+
const updatedRole = process.argv[3] ?? "1";
6+
7+
const updateUser = async () => {
8+
await connect();
9+
const userRepository = connection!.getRepository(User);
10+
userRepository.find({ id: userId }).then((user: any) => {
11+
if (!user.length) {
12+
console.error( "No user exists with the given id" )
13+
return;
14+
}
15+
const query = { id: user[0].id };
16+
const newValues = { user_role: updatedRole };
17+
userRepository
18+
.update(query, newValues)
19+
.then(() => console.log(`User updated successfully with role ${newValues.user_role}`)
20+
)
21+
.catch((err) => console.error(`error in updating user: ${err.message}`)
22+
);
23+
})
24+
.catch((err) => console.log(`error: ${err.message}`)
25+
)
26+
};
27+
28+
updateUser();

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.