Skip to content
Snippets Groups Projects
Commit 81c6f79a authored by bmate711's avatar bmate711
Browse files

Merge branch 'dev' into 'feature/news_api'

# Conflicts:
#   src/index.ts
#   tsconfig.json
parents bec0016b ba214b32
No related branches found
No related tags found
2 merge requests!10Feature/12 dev auto deploy,!6Feature/news api
This commit is part of merge request !6. Comments created here will be created in the context of that merge request.
...@@ -26,23 +26,18 @@ const app: Application = express(); ...@@ -26,23 +26,18 @@ const app: Application = express();
app.use(bodyParser.json()); // for parsing application/json app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
app.use( app.use(expressSession({
expressSession({ secret: process.env.SESSION_SECRET || "secret",
secret: process.env.SESSION_SECRET || "alma",
resave: false, resave: false,
saveUninitialized: true, saveUninitialized: true,
cookie: { secure: false } cookie: { secure: false }
}) }));
);
app.use((req: Request, res: Response, next: NextFunction) => {
res.data = {};
next();
});
app.get("/", (req: Request, res: Response) => { app.get("/", (req: Request, res: Response) => {
let message = "World!"; let message = "World!";
if (req.session!.user) message = req.session!.user!.sn || "World!"; if(req.session!.user)
res.send("Hello " + message); message = req.session!.user!.name || "World!";
res.send( "Hello " + message)
}); });
// Register routes // Register routes
......
...@@ -11,11 +11,15 @@ const complete = () => async (req: Request, res: Response) => { ...@@ -11,11 +11,15 @@ const complete = () => async (req: Request, res: Response) => {
}; };
try { try {
const result = await oauth2().authorizationCode.getToken(tokenConfig); const token = await oauth2().authorizationCode.getToken(tokenConfig);
const token = oauth2().accessToken.create(result); await axios.get(`https://auth.sch.bme.hu/api/profile/?access_token=${token.access_token}`)
await axios.get(`https://auth.sch.bme.hu/api/profile/?access_token=${token.token.access_token}`)
.then( (response) => { .then( (response) => {
req.session!.user = response.data; req.session!.user = {
id: String(response.data.basic),
email: String(response.data.mail),
name: `${response.data.sn} ${response.data.givenName}` ,
token,
};
}) })
.catch(function (error) { .catch(function (error) {
console.log(error); console.log(error);
......
import { Request, Response, NextFunction } from 'express';
import { oauth2 } from '../../utils/auth'
const refreshToken = () => async (req: Request, res: Response, next: NextFunction) => {
if (req.session && req.session.user) {
try{
let accessToken = oauth2().accessToken.create(req.session.user.token);
if (accessToken.expired(Number(process.env.AUTH_TOKEN_EXPIRATION_WINDOW_IN_SECONDS))) {
req.session.user.token = await accessToken.refresh().then(accessToken => accessToken.token);
}
}catch (error) {
console.log('Error refreshing access token: ', error.message);
}
}
next();
}
export default refreshToken;
\ No newline at end of file
import { Token } from "simple-oauth2";
export interface User{
email: string,
name: string,
id: string,
token: Token,
}
\ No newline at end of file
...@@ -2,9 +2,9 @@ import { Application } from 'express'; ...@@ -2,9 +2,9 @@ import { Application } from 'express';
import login from '../middlewares/auth/login'; import login from '../middlewares/auth/login';
import complete from '../middlewares/auth/complete'; import complete from '../middlewares/auth/complete';
import logout from '../middlewares/auth/logout'; import logout from '../middlewares/auth/logout';
import authenticated from '../middlewares/auth/authenticated' import authenticated from '../middlewares/auth/authenticated';
const authRout = (app: Application): void => { const authRoute = (app: Application): void => {
app.get('/login', login() ); app.get('/login', login() );
app.get('/complete', complete() ); app.get('/complete', complete() );
...@@ -12,4 +12,4 @@ import authenticated from '../middlewares/auth/authenticated' ...@@ -12,4 +12,4 @@ import authenticated from '../middlewares/auth/authenticated'
app.get('/logout', authenticated(), logout() ); app.get('/logout', authenticated(), logout() );
} }
export default authRout; export default authRoute;
\ No newline at end of file \ No newline at end of file
import { AccessToken } from "simple-oauth2";
import { User } from 'src/models/user.interface';
declare global {
namespace Express {
export interface Session {
user: User | null;
}
}
}
\ No newline at end of file
{ {
"files": [ "files": [
"./src/utils/declarations/response.d.ts", "./src/utils/declarations/response.d.ts",
"./src/utils/declarations/request.d.ts" "./src/utils/declarations/request.d.ts",
"./src/utils/declarations/session.d.ts",
], ],
"compilerOptions": { "compilerOptions": {
"module": "commonjs", "module": "commonjs",
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment