Additional progress towards completion!
This commit is contained in:
parent
d622cb0589
commit
2eebc4f79e
16 changed files with 215 additions and 52 deletions
|
|
@ -6,7 +6,7 @@ import { registerPostHandler } from './app/controllers/registerPostHandler'
|
|||
import { configGetHandler, configGetSpecificHandler } from './app/controllers/configGetHandler'
|
||||
import { configPutHandler } from './app/controllers/configPutHandler'
|
||||
import { UserModel } from './app/db/user.schema'
|
||||
import { auth } from './app/middleware'
|
||||
import { auth, optAuth } from './app/middleware'
|
||||
|
||||
interface AuthReq {
|
||||
user: UserModel | null
|
||||
|
|
@ -26,8 +26,8 @@ app.use(bodyParser.urlencoded({
|
|||
extended: true
|
||||
}))
|
||||
|
||||
app.put('/api/configs', optAuth, configPutHandler)
|
||||
app.get('/api/configs', auth, configGetHandler)
|
||||
app.put('/api/configs', auth, configPutHandler)
|
||||
app.get('/api/configs/:id', configGetSpecificHandler)
|
||||
app.post('/api/users/login', loginPostHandler)
|
||||
app.post('/api/users/register', registerPostHandler)
|
||||
|
|
|
|||
|
|
@ -7,10 +7,12 @@ export const configPutHandler = async (req: Request, res: Response) => {
|
|||
config.json = req.body.json
|
||||
await config.save()
|
||||
|
||||
req.user?.configs.push(config._id)
|
||||
await req.user.save()
|
||||
if(req.user) {
|
||||
req.user.configs.push(config._id)
|
||||
await req.user.save()
|
||||
}
|
||||
|
||||
return res.status(200).json(config.json)
|
||||
return res.status(200).send(config._id)
|
||||
} catch (ex) {
|
||||
console.error(ex)
|
||||
return res.status(500).send({ error: ex })
|
||||
|
|
|
|||
|
|
@ -3,6 +3,29 @@ import * as jwt from 'jsonwebtoken'
|
|||
import UserSchema from './db/user.schema'
|
||||
import moment from 'moment'
|
||||
|
||||
export const optAuth = async (req: Request, res: Response, next: NextFunction) => {
|
||||
const token = req.header('auth-token')
|
||||
|
||||
if (!token) {
|
||||
return next()
|
||||
}
|
||||
|
||||
try {
|
||||
const { _id, expiresAt } = (jwt.verify(token, process.env.JWT_SECRET as string) as { _id: string, expiresAt: string })
|
||||
|
||||
if (moment(moment()).isAfter(expiresAt)) {
|
||||
return next()
|
||||
}
|
||||
|
||||
const user = await UserSchema.findById(_id)
|
||||
req.user = user || null
|
||||
|
||||
return next()
|
||||
} catch (err) {
|
||||
return res.status(400).send(`Invalid Token: ${err}`)
|
||||
}
|
||||
}
|
||||
|
||||
export const auth = async (req: Request, res: Response, next: NextFunction) => {
|
||||
const token = req.header('auth-token')
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
import mongoose from 'mongoose'
|
||||
|
||||
export default async () => {
|
||||
await mongoose.connect(`mongodb://${process.env.MONGO_URI}/Blog`, {
|
||||
await mongoose.connect(`mongodb://${process.env.MONGO_URI}/GeolosysConfigGen`, {
|
||||
useNewUrlParser: true,
|
||||
useUnifiedTopology: true
|
||||
})
|
||||
|
|
|
|||
Loading…
Reference in a new issue