mirror of
https://github.com/myfatemi04/wheelshare-old-backend.git
synced 2025-04-21 12:10:17 -04:00
Merge branch 'main' of github.com:myfatemi04/Carpool-Backend into main
This commit is contained in:
commit
31966a8e65
|
@ -6,8 +6,8 @@ import {
|
||||||
getPoolsWithUser,
|
getPoolsWithUser,
|
||||||
getUserByID,
|
getUserByID,
|
||||||
groups,
|
groups,
|
||||||
users,
|
|
||||||
pools,
|
pools,
|
||||||
|
users,
|
||||||
} from './data';
|
} from './data';
|
||||||
|
|
||||||
export const router = Router();
|
export const router = Router();
|
||||||
|
@ -18,6 +18,11 @@ router.get('/user', (req, res) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
let userID = req.query.userID;
|
let userID = req.query.userID;
|
||||||
|
console.log(req.session, userID);
|
||||||
|
if (userID === '@me') {
|
||||||
|
userID = req.session.accountID;
|
||||||
|
}
|
||||||
|
|
||||||
let user = getUserByID(userID);
|
let user = getUserByID(userID);
|
||||||
|
|
||||||
if (user) {
|
if (user) {
|
||||||
|
|
24
src/auth.ts
24
src/auth.ts
|
@ -1,20 +1,11 @@
|
||||||
import * as simpleoauth2 from 'simple-oauth2';
|
|
||||||
import { v4 } from 'uuid';
|
import { v4 } from 'uuid';
|
||||||
import { getAccountIDFromIonCode } from './auth_ion';
|
import { getAccountIDFromIonCode } from './auth_ion';
|
||||||
|
|
||||||
const sessions: {
|
const sessions: {
|
||||||
// Maps to user ID
|
// Maps to user ID
|
||||||
[sessionID: string]: string;
|
[sessionID: string]: SessionData;
|
||||||
} = {};
|
} = {};
|
||||||
|
|
||||||
export function getUserIDFromSessionToken(sessionToken: string): string | null {
|
|
||||||
if (sessionToken in sessions) {
|
|
||||||
return sessions[sessionToken];
|
|
||||||
} else {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function createSessionFromCodeAndProvider(
|
export async function createSessionFromCodeAndProvider(
|
||||||
code: string,
|
code: string,
|
||||||
provider: 'ion'
|
provider: 'ion'
|
||||||
|
@ -26,10 +17,19 @@ export async function createSessionFromCodeAndProvider(
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the newly-created session ID
|
// Returns the newly-created session ID
|
||||||
export function createSession(userID: string): string {
|
export function createSession(accountID: string): string {
|
||||||
const id = v4();
|
const id = v4();
|
||||||
|
|
||||||
sessions[id] = userID;
|
sessions[id] = { accountID };
|
||||||
|
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function getSessionByToken(token: string): SessionData {
|
||||||
|
console.log(sessions);
|
||||||
|
if (token in sessions) {
|
||||||
|
return sessions[token];
|
||||||
|
} else {
|
||||||
|
return { accountID: null };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
10
src/getSessionID.ts
Normal file
10
src/getSessionID.ts
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
import { IncomingMessage } from 'http';
|
||||||
|
|
||||||
|
export default function getSessionID(request: IncomingMessage): string | null {
|
||||||
|
const auth = request.headers.authorization;
|
||||||
|
if (typeof auth === 'string' && auth.startsWith('Bearer ')) {
|
||||||
|
return auth.slice(7);
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
|
@ -17,11 +17,13 @@ import bodyParser from 'body-parser';
|
||||||
import cors from 'cors';
|
import cors from 'cors';
|
||||||
import express from 'express';
|
import express from 'express';
|
||||||
import * as api from './api';
|
import * as api from './api';
|
||||||
|
import { sessionMiddleware } from './sessionMiddleware';
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
|
|
||||||
app.use(cors());
|
app.use(cors());
|
||||||
app.use(bodyParser.json());
|
app.use(bodyParser.json());
|
||||||
|
app.use(sessionMiddleware);
|
||||||
|
|
||||||
app.use('/api', api.router);
|
app.use('/api', api.router);
|
||||||
|
|
||||||
|
|
15
src/sessionMiddleware.ts
Normal file
15
src/sessionMiddleware.ts
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
import { RequestHandler } from 'express';
|
||||||
|
import { getSessionByToken } from './auth';
|
||||||
|
import getSessionID from './getSessionID';
|
||||||
|
|
||||||
|
export const sessionMiddleware: RequestHandler = async (req, res, next) => {
|
||||||
|
const token = getSessionID(req);
|
||||||
|
|
||||||
|
if (token != null) {
|
||||||
|
req.session = getSessionByToken(token);
|
||||||
|
} else {
|
||||||
|
req.session = { accountID: null };
|
||||||
|
}
|
||||||
|
|
||||||
|
next();
|
||||||
|
};
|
13
src/typings/session.d.ts
vendored
Normal file
13
src/typings/session.d.ts
vendored
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
export {};
|
||||||
|
|
||||||
|
declare global {
|
||||||
|
interface SessionData {
|
||||||
|
accountID: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace Express {
|
||||||
|
interface Request {
|
||||||
|
session: SessionData;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,8 +2,9 @@
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"outDir": "dist/",
|
"outDir": "dist/",
|
||||||
"esModuleInterop": true,
|
"esModuleInterop": true,
|
||||||
|
"moduleResolution": "Node"
|
||||||
},
|
},
|
||||||
"files": ["src/types.ts"],
|
"files": ["src/types.ts", "src/typings/session.d.ts"],
|
||||||
"include": ["src/"],
|
"include": ["src/", "session.d.ts"],
|
||||||
"exclude": ["node_modules/"],
|
"exclude": ["node_modules/"]
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user