Second frontend, written in Next.JS + Typescript.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
1.4 KiB

4 years ago
  1. import getConfig from "next/config";
  2. import * as jwt from "jsonwebtoken";
  3. class Config {
  4. private data : ConfigData | null;
  5. constructor(data: ConfigData) {
  6. this.data = data;
  7. while (this.data.api && this.data.api.urlRoot.endsWith("/")) {
  8. this.data.api.urlRoot = this.data.api.urlRoot.slice(0, -1)
  9. }
  10. }
  11. auth0(): {clientId: string, clientSecret: string, domain: string} {
  12. return {
  13. clientId: this.data.auth0.clientId,
  14. clientSecret: this.data.auth0.secret,
  15. domain: this.data.auth0.domain,
  16. }
  17. }
  18. session(): ConfigDataSession {
  19. return this.data.session
  20. }
  21. apiUrl(path: string) {
  22. return this.data.api.urlRoot + path;
  23. }
  24. jwtToken(username: string, expiry: number = 15): string {
  25. return jwt.sign({
  26. user: username,
  27. exp: Math.floor((Date.now() / 1000) + expiry),
  28. }, this.data.api.secretKey, {
  29. header: {
  30. kid: this.data.api.secretId,
  31. }
  32. })
  33. }
  34. }
  35. export interface ConfigData {
  36. api: ConfigDataAPI
  37. auth0: ConfigDataAuth0
  38. session: ConfigDataSession
  39. redis: ConfigDataRedis
  40. }
  41. interface ConfigDataAPI {
  42. urlRoot: string
  43. secretId: string
  44. secretKey: string
  45. }
  46. interface ConfigDataAuth0 {
  47. secret: string
  48. clientId: string
  49. domain: string
  50. }
  51. interface ConfigDataSession {
  52. secret: string
  53. secure?: boolean
  54. }
  55. interface ConfigDataRedis {
  56. host: string
  57. port: number
  58. }
  59. const config = new Config(getConfig().serverRuntimeConfig);
  60. export default config;