forked from paranoidPhantom/tgauth
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
module.ts
55 lines (53 loc) · 1.63 KB
/
module.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { defineNuxtModule, createResolver, addComponent, addPlugin, addImports } from "@nuxt/kit";
import { defu } from "defu";
export interface ModuleOptions {
auth_expiration: number;
token?: string;
}
export default defineNuxtModule<ModuleOptions>({
meta: {
name: "tgauth",
configKey: "tgauth",
},
defaults: {
auth_expiration: 60 * 60 * 24 * 14, // Auth expiration in seconds, default is 2 weeks, 0 means no expiration
},
setup(_options, _nuxt) {
const resolver = createResolver(import.meta.url);
const token
= process.env.TG_BOT_TOKEN
?? _options.token
?? (_nuxt.options.runtimeConfig.tgauth as ModuleOptions).token;
if (!token) throw new Error("TG_BOT_TOKEN envirnoment variable is not set");
else
_nuxt.options.runtimeConfig.tgauth = defu(
_nuxt.options.runtimeConfig.tgauth as ModuleOptions,
{
token,
},
);
const expiration
= _options.auth_expiration
?? (_nuxt.options.runtimeConfig.tgauth as ModuleOptions).auth_expiration;
if (expiration)
_nuxt.options.runtimeConfig.tgauth = defu(
_nuxt.options.runtimeConfig.tgauth as ModuleOptions,
{
auth_expiration: expiration,
},
);
addPlugin(resolver.resolve("runtime/redirects"));
addImports({
name: "useTelegramUser",
from: resolver.resolve("runtime/composables/useTelegramUser"),
});
addComponent({
name: "TelegramAuth",
filePath: resolver.resolve("runtime/components/TelegramAuth.vue"),
});
_nuxt.hook("nitro:config", async (nitroConfig) => {
nitroConfig.alias = nitroConfig.alias || {};
nitroConfig.alias["#tgauth/server"] = await resolver.resolvePath("runtime/server/services");
});
},
});