-
-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
server: begin implementing header auth
- Loading branch information
Showing
13 changed files
with
170 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
// } | ||
// }) | ||
}) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
}) |