File tree 8 files changed +83
-23
lines changed
Filter options
8 files changed +83
-23
lines changed
Original file line number Diff line number Diff line change
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 =
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change @@ -203,6 +203,21 @@ authorization: JWT_TOKEN (returned by Login request)
203
203
204
204
<br />
205
205
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
+
206
221
## ✨ Run the Tests
207
222
208
223
``` yarn test ```
Original file line number Diff line number Diff line change 17
17
"lint" : " eslint src --ext .ts" ,
18
18
"build" : " tsc -p tsconfig.build.json" ,
19
19
"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"
21
22
},
22
23
"dependencies" : {
23
24
"axios" : " ^1.2.0" ,
Original file line number Diff line number Diff line change @@ -5,21 +5,15 @@ import {
5
5
} from '../services/session.service' ;
6
6
import { createUserWithToken } from '../services/user.service' ;
7
7
8
- const frontend_url = process . env . FRONTEND_URL ?? '/'
9
-
10
8
export const githubOauthHandler = async (
11
9
req : Request ,
12
10
res : Response ,
13
11
) => {
14
12
try {
15
13
const code = req . query . code as string ;
16
14
17
- if ( req . query . error ) {
18
- return res . redirect ( `${ frontend_url } /login` ) ;
19
- }
20
-
21
15
if ( ! code ) {
22
- console . log ( 'authorization code not provided' )
16
+ return res . json ( { error : 'authorization code not provided' } )
23
17
}
24
18
25
19
// Get access_token using code
@@ -29,11 +23,8 @@ export const githubOauthHandler = async (
29
23
const userData = await getGithubUser ( { access_token} ) ;
30
24
31
25
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 } )
37
28
38
29
} catch ( err : any ) {
39
30
res . json ( { 'error' : err . message } )
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -12,6 +12,7 @@ import { checkToken } from '../config/safeRoutes';
12
12
import ActiveSession from '../models/activeSession' ;
13
13
import User from '../models/user' ;
14
14
import { connection } from '../server/database' ;
15
+ import { logoutUser } from '../controllers/logout.controller' ;
15
16
16
17
// eslint-disable-next-line new-cap
17
18
const router = express . Router ( ) ;
@@ -115,16 +116,7 @@ router.post('/login', (req, res) => {
115
116
} ) ;
116
117
} ) ;
117
118
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 ) ;
128
120
129
121
router . post ( '/checkSession' , checkToken , ( _req , res ) => {
130
122
res . json ( { success : true } ) ;
Original file line number Diff line number Diff line change
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 ( ) ;
You can’t perform that action at this time.
0 commit comments