2021-06-24 10:09:10 +02:00
|
|
|
import { ADMIN_API_URL, ALLOW_ARTILLERY, SECRET_KEY } from "../Enum/EnvironmentVariable";
|
|
|
|
import { uuid } from "uuidv4";
|
2021-07-27 16:37:01 +02:00
|
|
|
import Jwt, { verify } from "jsonwebtoken";
|
2021-06-24 10:09:10 +02:00
|
|
|
import { TokenInterface } from "../Controller/AuthenticateController";
|
|
|
|
import { adminApi, AdminBannedData } from "../Services/AdminApi";
|
2020-11-13 18:00:22 +01:00
|
|
|
|
2021-07-27 16:37:01 +02:00
|
|
|
export interface AuthTokenData {
|
|
|
|
identifier: string; //will be a email if logged in or an uuid if anonymous
|
|
|
|
}
|
|
|
|
export const tokenInvalidException = "tokenInvalid";
|
|
|
|
|
2020-11-13 18:00:22 +01:00
|
|
|
class JWTTokenManager {
|
2021-07-27 16:37:01 +02:00
|
|
|
public createAuthToken(identifier: string) {
|
|
|
|
return Jwt.sign({ identifier }, SECRET_KEY, { expiresIn: "3d" });
|
2020-11-13 18:00:22 +01:00
|
|
|
}
|
|
|
|
|
2021-07-27 16:37:01 +02:00
|
|
|
public decodeJWTToken(token: string): AuthTokenData {
|
|
|
|
try {
|
|
|
|
return Jwt.verify(token, SECRET_KEY, { ignoreExpiration: false }) as AuthTokenData;
|
|
|
|
} catch (e) {
|
|
|
|
throw { reason: tokenInvalidException, message: e.message };
|
2020-11-13 18:00:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const jwtTokenManager = new JWTTokenManager();
|