Refactor auth logic.
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import { createMiddleware, unauthorized, badRequest, serverError } from 'next-basics';
|
||||
import cors from 'cors';
|
||||
import { getSession } from './session';
|
||||
import { getAuthToken } from './auth';
|
||||
import { getAuthToken, getShareToken } from './auth';
|
||||
|
||||
export const useCors = createMiddleware(cors());
|
||||
|
||||
@@ -27,11 +27,12 @@ export const useSession = createMiddleware(async (req, res, next) => {
|
||||
|
||||
export const useAuth = createMiddleware(async (req, res, next) => {
|
||||
const token = await getAuthToken(req);
|
||||
const shareToken = await getShareToken(req);
|
||||
|
||||
if (!token) {
|
||||
return unauthorized(res);
|
||||
}
|
||||
|
||||
req.auth = token;
|
||||
req.auth = { ...token, shareToken };
|
||||
next();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user