mirror of https://github.com/MaxLeiter/Drift
				
				
				
			server: begin implementing header auth
							parent
							
								
									6a951cad78
								
							
						
					
					
						commit
						13040ab8cc
					
				@ -0,0 +1,48 @@
 | 
			
		||||
import jwt, { UserJwtRequest } from "@lib/middleware/is-signed-in"
 | 
			
		||||
import { NextFunction, Response } from "express"
 | 
			
		||||
 | 
			
		||||
describe("jwt is-signed-in middlware", () => {
 | 
			
		||||
    let mockRequest: Partial<UserJwtRequest>
 | 
			
		||||
    let mockResponse: Partial<Response>
 | 
			
		||||
    let nextFunction: NextFunction = jest.fn()
 | 
			
		||||
 | 
			
		||||
    beforeEach(() => {
 | 
			
		||||
        mockRequest = {}
 | 
			
		||||
        mockResponse = {
 | 
			
		||||
            sendStatus: jest.fn().mockReturnThis()
 | 
			
		||||
        }
 | 
			
		||||
    })
 | 
			
		||||
 | 
			
		||||
    it("should return 401 if no authorization header", () => {
 | 
			
		||||
        const res = mockResponse as Response
 | 
			
		||||
        jwt(mockRequest as UserJwtRequest, res, nextFunction)
 | 
			
		||||
        expect(res.sendStatus).toHaveBeenCalledWith(401)
 | 
			
		||||
    })
 | 
			
		||||
 | 
			
		||||
    it("should return 401 if no token is supplied", () => {
 | 
			
		||||
        const req = mockRequest as UserJwtRequest
 | 
			
		||||
        req.headers = {
 | 
			
		||||
            authorization: "Bearer"
 | 
			
		||||
        }
 | 
			
		||||
        jwt(req, mockResponse as Response, nextFunction)
 | 
			
		||||
        expect(mockResponse.sendStatus).toBeCalledWith(401)
 | 
			
		||||
    })
 | 
			
		||||
 | 
			
		||||
    // it("should return 401 if token is deleted", async () => {
 | 
			
		||||
    //     try {
 | 
			
		||||
    //         const tokenString = "123"
 | 
			
		||||
 | 
			
		||||
    //         const req = mockRequest as UserJwtRequest
 | 
			
		||||
    //         req.headers = {
 | 
			
		||||
    //             authorization: `Bearer ${tokenString}`
 | 
			
		||||
    //         }
 | 
			
		||||
    //         jwt(req, mockResponse as Response, nextFunction)
 | 
			
		||||
    //         expect(mockResponse.sendStatus).toBeCalledWith(401)
 | 
			
		||||
    //         expect(mockResponse.json).toBeCalledWith({
 | 
			
		||||
    //             message: "Token is no longer valid"
 | 
			
		||||
    //         })
 | 
			
		||||
    //     } catch (e) {
 | 
			
		||||
    //         console.log(e)
 | 
			
		||||
    //     }
 | 
			
		||||
    // })
 | 
			
		||||
})
 | 
			
		||||
@ -1,48 +0,0 @@
 | 
			
		||||
import jwt, { UserJwtRequest } from "@lib/middleware/jwt"
 | 
			
		||||
import { NextFunction, Response } from "express"
 | 
			
		||||
 | 
			
		||||
describe("jwt middlware", () => {
 | 
			
		||||
	let mockRequest: Partial<UserJwtRequest>
 | 
			
		||||
	let mockResponse: Partial<Response>
 | 
			
		||||
	let nextFunction: NextFunction = jest.fn()
 | 
			
		||||
 | 
			
		||||
	beforeEach(() => {
 | 
			
		||||
		mockRequest = {}
 | 
			
		||||
		mockResponse = {
 | 
			
		||||
			sendStatus: jest.fn().mockReturnThis()
 | 
			
		||||
		}
 | 
			
		||||
	})
 | 
			
		||||
 | 
			
		||||
	it("should return 401 if no authorization header", () => {
 | 
			
		||||
		const res = mockResponse as Response
 | 
			
		||||
		jwt(mockRequest as UserJwtRequest, res, nextFunction)
 | 
			
		||||
		expect(res.sendStatus).toHaveBeenCalledWith(401)
 | 
			
		||||
	})
 | 
			
		||||
 | 
			
		||||
	it("should return 401 if no token is supplied", () => {
 | 
			
		||||
		const req = mockRequest as UserJwtRequest
 | 
			
		||||
		req.headers = {
 | 
			
		||||
			authorization: "Bearer"
 | 
			
		||||
		}
 | 
			
		||||
		jwt(req, mockResponse as Response, nextFunction)
 | 
			
		||||
		expect(mockResponse.sendStatus).toBeCalledWith(401)
 | 
			
		||||
	})
 | 
			
		||||
 | 
			
		||||
	// it("should return 401 if token is deleted", async () => {
 | 
			
		||||
	//     try {
 | 
			
		||||
	//         const tokenString = "123"
 | 
			
		||||
 | 
			
		||||
	//         const req = mockRequest as UserJwtRequest
 | 
			
		||||
	//         req.headers = {
 | 
			
		||||
	//             authorization: `Bearer ${tokenString}`
 | 
			
		||||
	//         }
 | 
			
		||||
	//         jwt(req, mockResponse as Response, nextFunction)
 | 
			
		||||
	//         expect(mockResponse.sendStatus).toBeCalledWith(401)
 | 
			
		||||
	//         expect(mockResponse.json).toBeCalledWith({
 | 
			
		||||
	//             message: "Token is no longer valid"
 | 
			
		||||
	//         })
 | 
			
		||||
	//     } catch (e) {
 | 
			
		||||
	//         console.log(e)
 | 
			
		||||
	//     }
 | 
			
		||||
	// })
 | 
			
		||||
})
 | 
			
		||||
@ -0,0 +1,31 @@
 | 
			
		||||
import { Router } from "express"
 | 
			
		||||
import jwt, { UserJwtRequest } from "@lib/middleware/is-signed-in"
 | 
			
		||||
import { User } from "@lib/models/User"
 | 
			
		||||
 | 
			
		||||
export const users = Router()
 | 
			
		||||
 | 
			
		||||
users.get("/self", jwt, async (req: UserJwtRequest, res, next) => {
 | 
			
		||||
	const error = () =>
 | 
			
		||||
		res.status(401).json({
 | 
			
		||||
			message: "Unauthorized"
 | 
			
		||||
		})
 | 
			
		||||
 | 
			
		||||
	try {
 | 
			
		||||
		if (!req.user) {
 | 
			
		||||
			return error()
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		const user = await User.findByPk(req.user?.id, {
 | 
			
		||||
			attributes: {
 | 
			
		||||
				exclude: ["password"]
 | 
			
		||||
			}
 | 
			
		||||
		})
 | 
			
		||||
		if (!user) {
 | 
			
		||||
			return error()
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		res.json(user)
 | 
			
		||||
	} catch (error) {
 | 
			
		||||
		next(error)
 | 
			
		||||
	}
 | 
			
		||||
})
 | 
			
		||||
					Loading…
					
					
				
		Reference in New Issue