typescript(multiplayer-backend): 😃 Started rewriting the auth code using stateless tokens.
typescript(multiplayer-backend): (now i hate JWTs, thanks to some random discrod user). typescript(multiplayer-backend): Most of it typescript(multiplayer-backend): doesnt work, its more of a prototype. Signed-off-by: prescientmoon <git@moonythm.dev>
This commit is contained in:
parent
da0a55fd84
commit
3740397789
|
@ -1,20 +1,12 @@
|
|||
import * as express from "express"
|
||||
import * as sessions from "express-session"
|
||||
import chalk from "chalk";
|
||||
|
||||
import { staticRoutes } from "../config";
|
||||
import { routes } from "./routes"
|
||||
import { urlencoded } from "body-parser";
|
||||
import { database } from "./services/db/firestore"
|
||||
import { morganChalk } from "./middleware/morgan";
|
||||
import { sessionMiddleware } from "./middleware/sessions"
|
||||
|
||||
|
||||
// @ts-ignore no declaration file
|
||||
// import * as store from "firestore-store"
|
||||
import * as store from "connect-mongo"
|
||||
import { connection, connected } from "./services/db/mongo";
|
||||
|
||||
// const firestore = store(sessions)
|
||||
export interface serverSetupResults {
|
||||
app: express.Application
|
||||
|
@ -23,19 +15,10 @@ export interface serverSetupResults {
|
|||
export const setupServer = (): Promise<serverSetupResults> =>
|
||||
new Promise(async (res, rej) => {
|
||||
try {
|
||||
let MongoStore = store(sessions)
|
||||
|
||||
await connected
|
||||
|
||||
//create express app
|
||||
const app = express()
|
||||
|
||||
app.use(urlencoded({ extended: true }), sessions({
|
||||
secret: process.env.SESSION_SECRET,
|
||||
saveUninitialized: false,
|
||||
resave: false,
|
||||
store: new MongoStore({ mongooseConnection: connection })
|
||||
}), morganChalk, sessionMiddleware)
|
||||
app.use(urlencoded({ extended: true }), morganChalk, sessionMiddleware)
|
||||
|
||||
//load static routes
|
||||
staticRoutes.forEach(route => {
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import { Response, Request } from "express";
|
||||
import { SessionDataDoc, SessionData } from "../../models/SessionData"
|
||||
|
||||
const getToken = (req: Request) => {
|
||||
if (req.headers.authorization && req.headers.authorization.split(' ')[0] === 'Bearer') { // Authorization: Bearer g1jipjgi1ifjioj
|
||||
|
@ -16,9 +17,54 @@ const getToken = (req: Request) => {
|
|||
return null;
|
||||
}
|
||||
|
||||
export const sessionMiddleware = (req: Request, res: Response, next: Function) => {
|
||||
export const sessionMiddleware = async (req: Request, res: Response, next: Function) => {
|
||||
const token = getToken(req)
|
||||
console.log(token)
|
||||
|
||||
//if we are trying to get an token, allow this
|
||||
if (req.path === "/token")
|
||||
return next()
|
||||
|
||||
//if we dont have any token
|
||||
if (!token)
|
||||
return res.json({ succes: false }).status(400)
|
||||
|
||||
//try searching for the object in the database
|
||||
const result = await SessionData.findOne({ token })
|
||||
|
||||
if (!result)
|
||||
return res.json({ succes: false }).status(400)
|
||||
|
||||
const data = JSON.parse(result.data)
|
||||
|
||||
if (!req.session)
|
||||
//@ts-ignore
|
||||
req.session = {}
|
||||
|
||||
for (let i in data)
|
||||
req.session[i] = data[i]
|
||||
|
||||
req.session.save = async () => {
|
||||
const toSave:any = {}
|
||||
|
||||
for (let i in req.session) {
|
||||
if (i == "save") continue
|
||||
|
||||
toSave[i] = req.session[i]
|
||||
}
|
||||
|
||||
const data:string = JSON.stringify(toSave)
|
||||
|
||||
return await result.updateOne({
|
||||
token,
|
||||
data
|
||||
})
|
||||
}
|
||||
|
||||
req.session.rainbow = "unicorn"
|
||||
|
||||
//TODO: remove types for express-session
|
||||
//@ts-ignore expects callback
|
||||
req.session.save()
|
||||
|
||||
next()
|
||||
}
|
9
typescript/multiplayer-backend/src/models/SessionData.ts
Normal file
9
typescript/multiplayer-backend/src/models/SessionData.ts
Normal file
|
@ -0,0 +1,9 @@
|
|||
import { createSchema, Type, typedModel, ExtractDoc } from "ts-mongoose"
|
||||
|
||||
const SessionDataSchema = createSchema({
|
||||
token: Type.string(),
|
||||
data: Type.string()
|
||||
})
|
||||
|
||||
export const SessionData = typedModel("SessionData", SessionDataSchema)
|
||||
export type SessionDataDoc = ExtractDoc<typeof SessionDataSchema>;
|
|
@ -33,14 +33,14 @@ const loginHtml = (req: Request, res: Response) => {
|
|||
<label for=name>name</label>
|
||||
<input type=name id=name name=name>
|
||||
</div>
|
||||
<button type=submit onclick="
|
||||
alert('click')
|
||||
<button type=submit>Submit</button>
|
||||
</form>
|
||||
<button onclick="
|
||||
fetch('/',{
|
||||
headers: {
|
||||
authorization: 'do u see this?'
|
||||
}
|
||||
})">Submit</button>
|
||||
</form>
|
||||
})">send</button>
|
||||
`)
|
||||
}
|
||||
|
||||
|
|
|
@ -1 +1,2 @@
|
|||
export * from "./auth"
|
||||
export * from "./token"
|
30
typescript/multiplayer-backend/src/routes/auth/token.ts
Normal file
30
typescript/multiplayer-backend/src/routes/auth/token.ts
Normal file
|
@ -0,0 +1,30 @@
|
|||
import { Router, Response, Request } from "express"
|
||||
import { randomBytes } from "crypto"
|
||||
import { SessionData,SessionDataDoc } from "../../models/SessionData";
|
||||
|
||||
const router = Router()
|
||||
|
||||
|
||||
const getToken = async (req: Request, res: Response) => {
|
||||
//generate token
|
||||
const token = randomBytes(16).toString("hex")
|
||||
|
||||
//save token into db
|
||||
const data = new SessionData({
|
||||
token,
|
||||
data:"{}"
|
||||
} as SessionDataDoc)
|
||||
|
||||
await data.save()
|
||||
|
||||
res.json({
|
||||
succes:true,
|
||||
data:{
|
||||
token
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
router.get("/", getToken)
|
||||
|
||||
export const token = router
|
|
@ -1,8 +1,9 @@
|
|||
import { auth } from "./auth"
|
||||
import { auth, token } from "./auth"
|
||||
import { logs } from "./logging"
|
||||
import { Router } from "express";
|
||||
|
||||
export const routes:{[key:string]:Router} = {
|
||||
auth,
|
||||
logs
|
||||
logs,
|
||||
token
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue