mirror of
https://github.com/myfatemi04/wheelshare-old-backend.git
synced 2025-04-18 10:50:18 -04:00
fix type issues
This commit is contained in:
parent
a53a982ec9
commit
d7f3b954d9
|
@ -6,8 +6,8 @@ import {
|
|||
getPoolsWithUser,
|
||||
getUserByID,
|
||||
groups,
|
||||
users,
|
||||
pools,
|
||||
users,
|
||||
} from './data';
|
||||
|
||||
export const router = Router();
|
||||
|
@ -18,6 +18,10 @@ router.get('/user', (req, res) => {
|
|||
}
|
||||
|
||||
let userID = req.query.userID;
|
||||
if (userID === '@me') {
|
||||
userID = req.session.accountID;
|
||||
}
|
||||
|
||||
let user = getUserByID(userID);
|
||||
|
||||
if (user) {
|
||||
|
|
19
src/auth.ts
19
src/auth.ts
|
@ -1,20 +1,11 @@
|
|||
import * as simpleoauth2 from 'simple-oauth2';
|
||||
import { v4 } from 'uuid';
|
||||
import { getAccountIDFromIonCode } from './auth_ion';
|
||||
|
||||
const sessions: {
|
||||
// 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(
|
||||
code: string,
|
||||
provider: 'ion'
|
||||
|
@ -26,10 +17,14 @@ export async function createSessionFromCodeAndProvider(
|
|||
}
|
||||
|
||||
// Returns the newly-created session ID
|
||||
export function createSession(userID: string): string {
|
||||
export function createSession(accountID: string): string {
|
||||
const id = v4();
|
||||
|
||||
sessions[id] = userID;
|
||||
sessions[id] = { accountID };
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
export function getSessionByToken(token: string): SessionData {
|
||||
return sessions[token];
|
||||
}
|
||||
|
|
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;
|
||||
}
|
||||
}
|
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": {
|
||||
"outDir": "dist/",
|
||||
"esModuleInterop": true,
|
||||
"moduleResolution": "Node"
|
||||
},
|
||||
"files": ["src/types.ts"],
|
||||
"include": ["src/"],
|
||||
"exclude": ["node_modules/"],
|
||||
}
|
||||
"files": ["src/types.ts", "src/typings/session.d.ts"],
|
||||
"include": ["src/", "session.d.ts"],
|
||||
"exclude": ["node_modules/"]
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user