Refactor auth logic.
This commit is contained in:
20
lib/auth.js
20
lib/auth.js
@@ -1,10 +1,10 @@
|
||||
import { validate } from 'uuid';
|
||||
import { parseSecureToken, parseToken, getItem } from 'next-basics';
|
||||
import { AUTH_TOKEN, SHARE_TOKEN_HEADER } from './constants';
|
||||
import { parseSecureToken, parseToken } from 'next-basics';
|
||||
import { getWebsite } from 'queries';
|
||||
import { secret } from './crypto';
|
||||
import { SHARE_TOKEN_HEADER } from 'lib/constants';
|
||||
import { secret } from 'lib/crypto';
|
||||
|
||||
export async function getAuthToken(req) {
|
||||
export function getAuthToken(req) {
|
||||
try {
|
||||
const token = req.headers.authorization;
|
||||
|
||||
@@ -14,13 +14,15 @@ export async function getAuthToken(req) {
|
||||
}
|
||||
}
|
||||
|
||||
export function getAuthHeader() {
|
||||
const token = getItem(AUTH_TOKEN);
|
||||
|
||||
return token ? { authorization: `Bearer ${token}` } : {};
|
||||
export function getShareToken(req) {
|
||||
try {
|
||||
return parseSecureToken(req.headers[SHARE_TOKEN_HEADER], secret());
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export async function isValidToken(token, validation) {
|
||||
export function isValidToken(token, validation) {
|
||||
try {
|
||||
const result = parseToken(token, secret());
|
||||
|
||||
|
||||
Reference in New Issue
Block a user