Skip to content

Commit

Permalink
ft(auth): added github auth
Browse files Browse the repository at this point in the history
  • Loading branch information
thepatrickniyo committed Apr 6, 2022
1 parent eec3fe0 commit 16d1ef2
Show file tree
Hide file tree
Showing 12 changed files with 79 additions and 27 deletions.
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ STACKEXCHANGE_CLIENT_ID=
STACKEXCHANGE_CLIENT_SECRET=
STACKEXCHANGE_APPS_KEY=
STACKEXCHANGE_API=https://api.stackexchange.com/2.3

GITHUB_CLIENT_ID=
GITHUB_CLIENT_SECRET=
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "index.js",
"scripts": {
"test": "mocha --config ./mocharc.jsonc --recursive --exit --timeout 30000",
"start": "node ./dist",
"start": "node ./dist",
"start:dev": "nodemon ./src/index.ts",
"build": "npm run clean && tsc -p tsconfig-build.json",
"prestart": "npm run build",
Expand All @@ -22,6 +22,7 @@
"module-alias": "^2.2.2",
"nodemon": "^2.0.15",
"passport": "^0.5.2",
"passport-github2": "^0.1.12",
"passport-stackapps-ts": "^1.0.3",
"typescript-ioc": "^3.2.2"
},
Expand All @@ -31,6 +32,7 @@
"@types/express-session": "^1.17.4",
"@types/mocha": "^8.2.0",
"@types/passport": "^1.0.7",
"@types/passport-github2": "^1.2.5",
"@types/passport-oauth2": "^1.4.11",
"@typescript-eslint/eslint-plugin": "^4.1.1",
"@typescript-eslint/parser": "^4.1.1",
Expand Down
8 changes: 5 additions & 3 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import 'module-alias/register';
import 'dotenv/config';
import express from 'express';
import session from 'express-session';
import passport from './strategies/stackoverflow.strategy';
import AuthRouter from './routes/auth.routes';
import passport from './strategies/passport.strategy';
import StackExchangeAuthRouter from './routes/stack-exchange-auth.routes';
import GithubAuthRouter from './routes/github-auth.routes';
import { SearchRouter } from './routes';
import { User } from 'User';

Expand All @@ -28,7 +29,8 @@ app.use(passport.session());
app.get('/', (req, res) => {
return res.status(200).json({ message: 'Welcome to the Stackoverflow Microservice API' });
});
app.use(`${API_PREFIX}/auth`, AuthRouter);
app.use(`${API_PREFIX}/auth/stack-exchange`, StackExchangeAuthRouter);
app.use(`${API_PREFIX}/auth/github`, GithubAuthRouter);
app.use(`${API_PREFIX}/search`, SearchRouter);

export default app;
6 changes: 3 additions & 3 deletions src/controllers/auth-controller/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import passport from "../../strategies/stackoverflow.strategy";
import passport from "../../strategies/passport.strategy";
import {Request, Response, NextFunction} from 'express';

export const login = (req:Request, res: Response, next:NextFunction): any => {
passport.authenticate('stack-exchange', function(err:any, user:any, info:any) {
export const login = (req:any, res: Response, next:NextFunction): any => {
passport.authenticate(req.passportStrategyType, function(err:any, user:any, info:any) {
if (err) {
return next(err);
}
Expand Down
Empty file added src/middlewares/auth/.gitkeep
Empty file.
11 changes: 11 additions & 0 deletions src/middlewares/passport/passport-strategy-type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {NextFunction, Response} from 'express';

export const GithubStrategyType = (req: any, _: Response, next: NextFunction) => {
req.passportStrategyType = 'github';
next();
}

export const StackExchangeStrategyType = (req: any, _: Response, next: NextFunction) => {
req.passportStrategyType = 'stack-exchange';
next();
}
13 changes: 0 additions & 13 deletions src/routes/auth.routes.ts

This file was deleted.

14 changes: 14 additions & 0 deletions src/routes/github-auth.routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Router } from 'express';
import { GithubStrategyType } from '../middlewares/passport/passport-strategy-type';
import { login, logout } from '../controllers/auth-controller';
import passport from '../strategies/passport.strategy';

const router = Router();

router.get('/',passport.authenticate('github'));

router.get('/callback', GithubStrategyType,login);

router.get('/logout', logout);

export default router;
5 changes: 3 additions & 2 deletions src/routes/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './auth.routes';
export * from './search.routes';
export * from './stack-exchange-auth.routes';
export * from './search.routes';
export * from './github-auth.routes'
14 changes: 14 additions & 0 deletions src/routes/stack-exchange-auth.routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Router } from 'express';
import { StackExchangeStrategyType } from '../middlewares/passport/passport-strategy-type';
import { login, logout } from '../controllers/auth-controller';
import passport from '../strategies/passport.strategy';

const router = Router();

router.get('/',passport.authenticate('stack-exchange'));

router.get('/callback', StackExchangeStrategyType, login);

router.get('/logout', logout);

export default router;
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import 'dotenv/config';
import passport from 'passport';
import { Strategy } from "passport-stackapps-ts";
import { Strategy as StackExchangeStrategy } from "passport-stackapps-ts";
import { Strategy as GitHubStrategy} from 'passport-github2';
import { Profile } from 'Profile';

const STACKEXCHANGE_CLIENT_ID = process.env.STACKEXCHANGE_CLIENT_ID;
const STACKEXCHANGE_CLIENT_SECRET = process.env.STACKEXCHANGE_CLIENT_SECRET;
const STACKEXCHANGE_APPS_KEY = process.env.STACKEXCHANGE_APPS_KEY;
passport.use(new Strategy({
const STACKEXCHANGE_CLIENT_ID = process.env.STACKEXCHANGE_CLIENT_ID as string;
const STACKEXCHANGE_CLIENT_SECRET = process.env.STACKEXCHANGE_CLIENT_SECRET as string;
const STACKEXCHANGE_APPS_KEY = process.env.STACKEXCHANGE_APPS_KEY as string;

const GITHUB_CLIENT_ID = process.env.GITHUB_CLIENT_ID as string;
const GITHUB_CLIENT_SECRET = process.env.GITHUB_CLIENT_SECRET as string;

passport.use(new StackExchangeStrategy({
clientID: STACKEXCHANGE_CLIENT_ID,
clientSecret: STACKEXCHANGE_CLIENT_SECRET,
callbackURL: `http://${process.env.HOST}:${process.env.PORT}/api/v1/auth/stack-exchange/callback`,
Expand All @@ -21,4 +26,16 @@ passport.use(new Strategy({
}
));

passport.use(new GitHubStrategy({
clientID: GITHUB_CLIENT_ID,
clientSecret: GITHUB_CLIENT_SECRET,
callbackURL: `http://${process.env.HOST}:${process.env.PORT}/api/v1/auth/github/callback`,
},
function(accessToken: string, refreshToken: string, profile:Profile, done: any) {
process.nextTick(function () {
return done(null, {profile, accessToken, refreshToken});
});
}
));

export default passport;
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"compilerOptions": {
"module": "commonjs",
"esModuleInterop": true,
"skipLibCheck":true,
"target": "es6",
"noImplicitAny": true,
"moduleResolution": "node",
Expand Down

0 comments on commit 16d1ef2

Please sign in to comment.