@lobb-js/lobb-ext-auth 0.1.66 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.vscode/settings.json +5 -0
- package/CHANGELOG.md +86 -27
- package/README.md +1 -1
- package/extensions/auth/collections/activityFeed.ts +53 -0
- package/extensions/auth/collections/collections.ts +43 -0
- package/extensions/auth/collections/sessions.ts +34 -0
- package/extensions/auth/collections/users.ts +44 -0
- package/extensions/auth/config/extensionConfigSchema.ts +47 -0
- package/extensions/auth/config/permissionsAction/create.ts +20 -0
- package/extensions/auth/config/permissionsAction/delete.ts +3 -0
- package/extensions/auth/config/permissionsAction/read.ts +10 -0
- package/extensions/auth/config/permissionsAction/update.ts +20 -0
- package/extensions/auth/database/init.ts +51 -0
- package/extensions/auth/database/migrations.ts +3 -0
- package/extensions/auth/database/utils.ts +36 -0
- package/extensions/auth/index.ts +19 -0
- package/extensions/auth/meta/meta.ts +11 -0
- package/extensions/auth/openapi.ts +469 -0
- package/extensions/auth/studio/tests/login.spec.ts +34 -0
- package/extensions/auth/studio/tests/package.json +1 -0
- package/extensions/auth/studio/tests/playwright.config.cjs +27 -0
- package/extensions/auth/tests/collections/extend_users_collection.test.ts +61 -0
- package/extensions/auth/tests/configs/auth.ts +73 -0
- package/extensions/auth/tests/configs/auth_no_roles.ts +65 -0
- package/extensions/auth/tests/configs/auth_public_full_access.ts +69 -0
- package/extensions/auth/tests/configs/auth_with_different_admin_creds.ts +81 -0
- package/extensions/auth/tests/configs/auth_with_extend_users.ts +81 -0
- package/extensions/auth/tests/configs/auth_with_refresh_token.ts +86 -0
- package/extensions/auth/tests/configs/auth_with_short_access_token_only.ts +95 -0
- package/extensions/auth/tests/configs/auth_with_short_time_refresh_token.ts +86 -0
- package/extensions/auth/tests/configs/social_blog.ts +156 -0
- package/extensions/auth/tests/controllers/change_password.test.ts +113 -0
- package/extensions/auth/tests/controllers/dashboardAccessRoles.test.ts +29 -0
- package/extensions/auth/tests/controllers/login.test.ts +101 -0
- package/extensions/auth/tests/controllers/logout.test.ts +89 -0
- package/extensions/auth/tests/controllers/me.test.ts +272 -0
- package/extensions/auth/tests/controllers/register.test.ts +45 -0
- package/extensions/auth/tests/database/db.test.ts +64 -0
- package/extensions/auth/tests/database/differentAdminCreds.test.ts +51 -0
- package/extensions/auth/tests/middlewares/adminAuthGuard.test.ts +157 -0
- package/extensions/auth/tests/middlewares/publicAllowBasic.test.ts +137 -0
- package/extensions/auth/tests/middlewares/publicPreventBasic.test.ts +108 -0
- package/extensions/auth/tests/socialBlog.test.ts +253 -0
- package/extensions/auth/tests/utils/addArticles.ts +22 -0
- package/extensions/auth/tests/utils/addSocialBlogArticles.ts +52 -0
- package/extensions/auth/tests/utils/data/articles.ts +65 -0
- package/extensions/auth/tests/utils/data/socialBlogArticles.ts +56 -0
- package/extensions/auth/utils.ts +17 -0
- package/extensions/auth/workflows/baseWorkflow.ts +159 -0
- package/extensions/auth/workflows/hashHandlerWorkflows.ts +29 -0
- package/extensions/auth/workflows/index.ts +29 -0
- package/extensions/auth/workflows/meAliasWorkflows.ts +48 -0
- package/extensions/auth/workflows/policiesWorkflows.ts +240 -0
- package/extensions/auth/workflows/utils.ts +306 -0
- package/lobb.ts +108 -0
- package/package.json +34 -6
- package/public/public/vite.svg +1 -0
- package/scripts/postpublish.sh +12 -0
- package/scripts/prepublish.sh +17 -0
- package/studio/app.html +12 -0
- package/studio/routes/+layout.svelte +7 -0
- package/studio/routes/+layout.ts +1 -0
- package/studio/routes/[...path]/+page.svelte +6 -0
- package/svelte.config.js +23 -7
- package/todo.md +37 -0
- package/tsconfig.app.json +3 -3
- package/tsconfig.json +9 -10
- package/vite.config.ts +4 -10
- package/.env +0 -1
- package/components.json +0 -16
- package/index.html +0 -13
- package/src/app.css +0 -124
- package/src/main.ts +0 -14
- /package/{src → extensions/auth/studio}/auth.ts +0 -0
- /package/{src → extensions/auth/studio}/index.ts +0 -0
- /package/{src → extensions/auth/studio}/lib/components/pages/loginPage/index.svelte +0 -0
- /package/{src → extensions/auth/studio}/lib/components/pages/settings/index.svelte +0 -0
- /package/{src → extensions/auth/studio}/lib/components/pages/settings/pages/activityFeed.svelte +0 -0
- /package/{src → extensions/auth/studio}/lib/components/pages/settings/pages/rolesAndPermissions.svelte +0 -0
- /package/{src → extensions/auth/studio}/lib/components/pages/settings/pages/users.svelte +0 -0
- /package/{src → extensions/auth/studio}/lib/components/pages/userSettings/components/account.svelte +0 -0
- /package/{src → extensions/auth/studio}/lib/components/pages/userSettings/components/profile.svelte +0 -0
- /package/{src → extensions/auth/studio}/lib/components/pages/userSettings/index.svelte +0 -0
- /package/{src → extensions/auth/studio}/lib/index.ts +0 -0
- /package/{src → extensions/auth/studio}/lib/utils.ts +0 -0
- /package/{src → extensions/auth/studio}/onStartup.ts +0 -0
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { Lobb } from "@lobb-js/core";
|
|
2
|
+
import { articles } from "../utils/data/articles.ts";
|
|
3
|
+
|
|
4
|
+
export async function createArticles(lobb: Lobb) {
|
|
5
|
+
const createdArticles: any[] = [];
|
|
6
|
+
for (let index = 0; index < articles.length; index++) {
|
|
7
|
+
const article = articles[index];
|
|
8
|
+
const createdArticle = (await lobb.collectionService.createOne({
|
|
9
|
+
collectionName: "articles",
|
|
10
|
+
data: article,
|
|
11
|
+
})).data;
|
|
12
|
+
createdArticles.push(createdArticle);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return createdArticles.map((article) => article.id);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export async function removeArticles(lobb: Lobb) {
|
|
19
|
+
await lobb.collectionService.deleteMany({
|
|
20
|
+
collectionName: "articles",
|
|
21
|
+
});
|
|
22
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { Lobb } from "@lobb-js/core";
|
|
2
|
+
import { articles } from "./data/socialBlogArticles.ts";
|
|
3
|
+
|
|
4
|
+
export async function createArticles(lobb: Lobb) {
|
|
5
|
+
// clean collections
|
|
6
|
+
await removeArticles(lobb);
|
|
7
|
+
|
|
8
|
+
// create the owner user
|
|
9
|
+
const user = (await lobb.collectionService.createOne({
|
|
10
|
+
collectionName: "auth_users",
|
|
11
|
+
data: {
|
|
12
|
+
email: "john@gmail.com",
|
|
13
|
+
password: "123456",
|
|
14
|
+
role: "author",
|
|
15
|
+
},
|
|
16
|
+
})).data;
|
|
17
|
+
const user2 = (await lobb.collectionService.createOne({
|
|
18
|
+
collectionName: "auth_users",
|
|
19
|
+
data: {
|
|
20
|
+
email: "doe@gmail.com",
|
|
21
|
+
password: "123456",
|
|
22
|
+
role: "author",
|
|
23
|
+
},
|
|
24
|
+
})).data;
|
|
25
|
+
|
|
26
|
+
// create the articles
|
|
27
|
+
const createdArticles: any[] = [];
|
|
28
|
+
for (let index = 0; index < articles.length; index++) {
|
|
29
|
+
const article: any = articles[index];
|
|
30
|
+
article["author_id"] = index > (articles.length / 2) ? user2.id : user.id;
|
|
31
|
+
const createdArticle = (await lobb.collectionService.createOne({
|
|
32
|
+
collectionName: "articles",
|
|
33
|
+
data: article,
|
|
34
|
+
})).data;
|
|
35
|
+
createdArticles.push(createdArticle);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return createdArticles.map((article) => article.id);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export async function removeArticles(lobb: Lobb) {
|
|
42
|
+
await lobb.collectionService.deleteMany({
|
|
43
|
+
collectionName: "articles",
|
|
44
|
+
filter: {},
|
|
45
|
+
force: true,
|
|
46
|
+
});
|
|
47
|
+
await lobb.collectionService.deleteMany({
|
|
48
|
+
collectionName: "auth_users",
|
|
49
|
+
filter: {},
|
|
50
|
+
force: true,
|
|
51
|
+
});
|
|
52
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
export const articles = [
|
|
2
|
+
{
|
|
3
|
+
title: "The Future of AI: Trends Shaping Tomorrow's Technology",
|
|
4
|
+
body:
|
|
5
|
+
"Explore how artificial intelligence is evolving, from machine learning innovations to ethical challenges in AI governance. This article delves into the transformative potential AI holds for industries like healthcare, finance, and automation.",
|
|
6
|
+
number_of_likes: 454,
|
|
7
|
+
published: null,
|
|
8
|
+
},
|
|
9
|
+
{
|
|
10
|
+
title: "Climate Change and Its Impact on Global Food Security",
|
|
11
|
+
body:
|
|
12
|
+
"This article examines the effect of climate change on agriculture and global food production. It covers rising temperatures, changing weather patterns, and their implications for food supply chains, emphasizing the need for sustainable farming practices Wow.",
|
|
13
|
+
number_of_likes: 1212,
|
|
14
|
+
},
|
|
15
|
+
{
|
|
16
|
+
title: "The Rise of Remote Work: Is the Office Dead?",
|
|
17
|
+
body:
|
|
18
|
+
"A comprehensive analysis of How remote work has redefined the modern workplace post-pandemic. It discusses benefits and challenges for both employers and employees, and whether traditional office spaces will continue to exist in the future.",
|
|
19
|
+
number_of_likes: 9848,
|
|
20
|
+
published: true,
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
title: "Blockchain Beyond Cryptocurrency: Practical Applications in 2024",
|
|
24
|
+
body:
|
|
25
|
+
"Blockchain technology extends far beyond Bitcoin and digital currencies. This article explains its real-world uses in supply chain management, healthcare, and secure voting systems, highlighting successful implementations and ongoing experiments.",
|
|
26
|
+
number_of_likes: 10,
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
title: "Mental Health in the Digital Age: Navigating the New Challenges",
|
|
30
|
+
body:
|
|
31
|
+
"With increasing screen time and social media usage, mental health issues have taken new forms. This article addresses the growing concerns around digital addiction, online anxiety, and the role of tech companies in promoting well-being wow.",
|
|
32
|
+
number_of_likes: 5,
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
title: "Understanding Quantum Computing: The Next Tech Revolution",
|
|
36
|
+
body:
|
|
37
|
+
"Quantum computing promises to solve problems too complex for classical computers. This article breaks down the fundamentals of quantum technology, its current state, and its future potential across industries like pharmaceuticals, logistics, and cybersecurity.",
|
|
38
|
+
number_of_likes: 156,
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
title: "Sustainable Fashion: How the Industry is Going Green",
|
|
42
|
+
body:
|
|
43
|
+
"With environmental consciousness on the rise, the fashion industry is pivoting toward sustainability. This article looks at eco-friendly materials, ethical manufacturing processes, and how major brands are embracing the green movement wow.",
|
|
44
|
+
number_of_likes: 89,
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
title: "The Role of 5G in Revolutionizing Smart Cities",
|
|
48
|
+
body:
|
|
49
|
+
"As 5G networks roll out globally, smart cities are becoming a reality. This wow article covers how 5G technology wow will enable real-time data sharing, improve urban infrastructure, wow and enhance public services through IoT devices and smart sensors.",
|
|
50
|
+
number_of_likes: 15,
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
title: "Digital Marketing in 2024: Emerging Strategies for Success",
|
|
54
|
+
body:
|
|
55
|
+
"The landscape of digital marketing is rapidly changing with the rise of AI, personalized experiences, and social commerce. This article outlines the most promising strategies to stay ahead in 2024, from leveraging data analytics to mastering content creation wow.",
|
|
56
|
+
number_of_likes: 87,
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
title:
|
|
60
|
+
"The Future of Renewable Energy: Innovations Driving a Clean Energy Revolution",
|
|
61
|
+
body:
|
|
62
|
+
"As the world shifts toward green energy, this article highlights wow the latest innovations in solar, wind, and hydropower technologies. It also discusses policy trends, wow global investment in renewables, wow and the challenges in achieving net-zero emissions by 2050 wow.",
|
|
63
|
+
number_of_likes: 48,
|
|
64
|
+
},
|
|
65
|
+
];
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
export const articles = [
|
|
2
|
+
{
|
|
3
|
+
title: "The Future of AI: Trends Shaping Tomorrow's Technology",
|
|
4
|
+
body:
|
|
5
|
+
"Explore how artificial intelligence is evolving, from machine learning innovations to ethical challenges in AI governance. This article delves into the transformative potential AI holds for industries like healthcare, finance, and automation.",
|
|
6
|
+
status: "public",
|
|
7
|
+
},
|
|
8
|
+
{
|
|
9
|
+
title: "Climate Change and Its Impact on Global Food Security",
|
|
10
|
+
body:
|
|
11
|
+
"This article examines the effect of climate change on agriculture and global food production. It covers rising temperatures, changing weather patterns, and their implications for food supply chains, emphasizing the need for sustainable farming practices Wow.",
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
title: "The Rise of Remote Work: Is the Office Dead?",
|
|
15
|
+
body:
|
|
16
|
+
"A comprehensive analysis of How remote work has redefined the modern workplace post-pandemic. It discusses benefits and challenges for both employers and employees, and whether traditional office spaces will continue to exist in the future.",
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
title: "Blockchain Beyond Cryptocurrency: Practical Applications in 2024",
|
|
20
|
+
body:
|
|
21
|
+
"Blockchain technology extends far beyond Bitcoin and digital currencies. This article explains its real-world uses in supply chain management, healthcare, and secure voting systems, highlighting successful implementations and ongoing experiments.",
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
title: "Mental Health in the Digital Age: Navigating the New Challenges",
|
|
25
|
+
body:
|
|
26
|
+
"With increasing screen time and social media usage, mental health issues have taken new forms. This article addresses the growing concerns around digital addiction, online anxiety, and the role of tech companies in promoting well-being wow.",
|
|
27
|
+
status: "public",
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
title: "Understanding Quantum Computing: The Next Tech Revolution",
|
|
31
|
+
body:
|
|
32
|
+
"Quantum computing promises to solve problems too complex for classical computers. This article breaks down the fundamentals of quantum technology, its current state, and its future potential across industries like pharmaceuticals, logistics, and cybersecurity.",
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
title: "Sustainable Fashion: How the Industry is Going Green",
|
|
36
|
+
body:
|
|
37
|
+
"With environmental consciousness on the rise, the fashion industry is pivoting toward sustainability. This article looks at eco-friendly materials, ethical manufacturing processes, and how major brands are embracing the green movement wow.",
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
title: "The Role of 5G in Revolutionizing Smart Cities",
|
|
41
|
+
body:
|
|
42
|
+
"As 5G networks roll out globally, smart cities are becoming a reality. This wow article covers how 5G technology wow will enable real-time data sharing, improve urban infrastructure, wow and enhance public services through IoT devices and smart sensors.",
|
|
43
|
+
status: "public",
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
title: "Digital Marketing in 2024: Emerging Strategies for Success",
|
|
47
|
+
body:
|
|
48
|
+
"The landscape of digital marketing is rapidly changing with the rise of AI, personalized experiences, and social commerce. This article outlines the most promising strategies to stay ahead in 2024, from leveraging data analytics to mastering content creation wow.",
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
title:
|
|
52
|
+
"The Future of Renewable Energy: Innovations Driving a Clean Energy Revolution",
|
|
53
|
+
body:
|
|
54
|
+
"As the world shifts toward green energy, this article highlights wow the latest innovations in solar, wind, and hydropower technologies. It also discusses policy trends, wow global investment in renewables, wow and the challenges in achieving net-zero emissions by 2050 wow.",
|
|
55
|
+
},
|
|
56
|
+
];
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { Context } from "hono";
|
|
2
|
+
|
|
3
|
+
export function getBearerToken(c: Context): string | null {
|
|
4
|
+
const authHeader = c.req.header("Authorization");
|
|
5
|
+
if (!authHeader) return null;
|
|
6
|
+
|
|
7
|
+
const [scheme, token] = authHeader.split(" ");
|
|
8
|
+
if (scheme !== "Bearer" || !token) return null;
|
|
9
|
+
|
|
10
|
+
return token;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function generateRandomId(): string {
|
|
14
|
+
const array = new Uint8Array(32);
|
|
15
|
+
crypto.getRandomValues(array);
|
|
16
|
+
return Array.from(array).map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
17
|
+
}
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import type { Workflow } from "@lobb-js/core";
|
|
2
|
+
import type { Context } from "hono";
|
|
3
|
+
import type { User } from "../config/extensionConfigSchema.ts";
|
|
4
|
+
import { generateRandomId, getBearerToken } from "../utils.ts";
|
|
5
|
+
import { verify } from "argon2";
|
|
6
|
+
|
|
7
|
+
export const baseWorkflows: Workflow[] = [
|
|
8
|
+
{
|
|
9
|
+
name: "auth_BearerTokenHandler",
|
|
10
|
+
eventName: "core.webserver.middlwares.pre",
|
|
11
|
+
handler: async (input, ctx) => {
|
|
12
|
+
const token = getBearerToken(input.context);
|
|
13
|
+
if (token) {
|
|
14
|
+
const sessionsResult = await ctx.workflows.collectionService({
|
|
15
|
+
method: "findAll",
|
|
16
|
+
props: {
|
|
17
|
+
collectionName: "auth_sessions",
|
|
18
|
+
params: {
|
|
19
|
+
filter: {
|
|
20
|
+
token,
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
},
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
const session = sessionsResult.data[0];
|
|
27
|
+
|
|
28
|
+
if (session) {
|
|
29
|
+
const usersResult = await ctx.workflows.collectionService({
|
|
30
|
+
method: "findAll",
|
|
31
|
+
props: {
|
|
32
|
+
collectionName: "auth_users",
|
|
33
|
+
params: {
|
|
34
|
+
filter: {
|
|
35
|
+
id: session.user_id,
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
const user = usersResult.data[0];
|
|
41
|
+
|
|
42
|
+
const context = input.context as Context;
|
|
43
|
+
context.set("auth_session", session);
|
|
44
|
+
context.set("auth_user", user);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
},
|
|
49
|
+
// auth_logins workflows
|
|
50
|
+
{
|
|
51
|
+
name: "auth_userLoginsHandler",
|
|
52
|
+
eventName: "core.controllers.preCreateOne",
|
|
53
|
+
handler: async (input, ctx) => {
|
|
54
|
+
if (input.collectionName === "auth_sessions") {
|
|
55
|
+
const payloadEmail = input.body.data.email;
|
|
56
|
+
const payloadPassword = input.body.data.password;
|
|
57
|
+
|
|
58
|
+
const users = await ctx.workflows.collectionService({
|
|
59
|
+
method: "findAll",
|
|
60
|
+
props: {
|
|
61
|
+
collectionName: "auth_users",
|
|
62
|
+
params: {
|
|
63
|
+
filter: {
|
|
64
|
+
email: payloadEmail,
|
|
65
|
+
},
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
if (!users.data.length) {
|
|
71
|
+
throw new ctx.LobbError({
|
|
72
|
+
code: "NOT_FOUND",
|
|
73
|
+
message: `The user with this email (${payloadEmail}) doesnt exist.`,
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const user = users.data[0] as User;
|
|
78
|
+
const hashedPassword = user.password;
|
|
79
|
+
|
|
80
|
+
const passwordIsCorrect = await verify(
|
|
81
|
+
hashedPassword,
|
|
82
|
+
payloadPassword,
|
|
83
|
+
);
|
|
84
|
+
if (!passwordIsCorrect) {
|
|
85
|
+
throw new ctx.LobbError({
|
|
86
|
+
code: "UNAUTHORIZED",
|
|
87
|
+
message:
|
|
88
|
+
"The password provided is incorrect. Please verify and try again.",
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const sessionPayload = {
|
|
93
|
+
token: generateRandomId(),
|
|
94
|
+
expires_at: new Date(Date.now() + 3600 * 1000),
|
|
95
|
+
user_id: user.id,
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
const sessionResult = await ctx.workflows.collectionService({
|
|
99
|
+
method: "createOne",
|
|
100
|
+
props: {
|
|
101
|
+
collectionName: "auth_sessions",
|
|
102
|
+
data: sessionPayload,
|
|
103
|
+
},
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
throw new Response(
|
|
107
|
+
JSON.stringify({
|
|
108
|
+
data: {
|
|
109
|
+
access_token: {
|
|
110
|
+
token: sessionPayload.token,
|
|
111
|
+
expires_at: sessionResult.data.expires_at,
|
|
112
|
+
},
|
|
113
|
+
user: {
|
|
114
|
+
id: user.id,
|
|
115
|
+
email: user.email,
|
|
116
|
+
role: user.role,
|
|
117
|
+
},
|
|
118
|
+
},
|
|
119
|
+
}),
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
},
|
|
123
|
+
},
|
|
124
|
+
{
|
|
125
|
+
name: "auth_userLogoutHandler",
|
|
126
|
+
eventName: "core.controllers.preDeleteMany",
|
|
127
|
+
handler: async (input, ctx) => {
|
|
128
|
+
if (input.collectionName === "auth_sessions") {
|
|
129
|
+
const context = input.context as Context;
|
|
130
|
+
const token = getBearerToken(context);
|
|
131
|
+
|
|
132
|
+
if (!token) {
|
|
133
|
+
throw new ctx.LobbError({
|
|
134
|
+
code: "BAD_REQUEST",
|
|
135
|
+
message:
|
|
136
|
+
"You should pass session's token through the bearer header",
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
await ctx.workflows.collectionService({
|
|
141
|
+
method: "deleteMany",
|
|
142
|
+
props: {
|
|
143
|
+
collectionName: "auth_sessions",
|
|
144
|
+
filter: {
|
|
145
|
+
token: token,
|
|
146
|
+
},
|
|
147
|
+
},
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
throw new Response(
|
|
151
|
+
null,
|
|
152
|
+
{
|
|
153
|
+
status: 204,
|
|
154
|
+
},
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
},
|
|
158
|
+
},
|
|
159
|
+
];
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { Workflow } from "@lobb-js/core";
|
|
2
|
+
import { hash } from "argon2";
|
|
3
|
+
|
|
4
|
+
export const hashHandlerWorkflows: Workflow[] = [
|
|
5
|
+
// hash passwords on user create
|
|
6
|
+
{
|
|
7
|
+
name: "auth_hashPasswordWorkflow",
|
|
8
|
+
eventName: "core.store.preCreateOne",
|
|
9
|
+
handler: async (input) => {
|
|
10
|
+
if (input.collectionName === "auth_users") {
|
|
11
|
+
input.data.password = await hash(input.data.password);
|
|
12
|
+
}
|
|
13
|
+
return input;
|
|
14
|
+
},
|
|
15
|
+
},
|
|
16
|
+
// hash passwords on user update
|
|
17
|
+
{
|
|
18
|
+
name: "auth_hashPasswordWorkflowForUpdate",
|
|
19
|
+
eventName: "core.store.preUpdateOne",
|
|
20
|
+
handler: async (input) => {
|
|
21
|
+
if (input.collectionName === "auth_users") {
|
|
22
|
+
if (input.data.password) {
|
|
23
|
+
input.data.password = await hash(input.data.password);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return input;
|
|
27
|
+
},
|
|
28
|
+
},
|
|
29
|
+
];
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { Workflow } from "@lobb-js/core";
|
|
2
|
+
import type { ExtensionConfig } from "../config/extensionConfigSchema.ts";
|
|
3
|
+
import { getPoliciesWorkflows } from "./policiesWorkflows.ts";
|
|
4
|
+
import { meAliasWorkflows } from "./meAliasWorkflows.ts";
|
|
5
|
+
import { baseWorkflows } from "./baseWorkflow.ts";
|
|
6
|
+
import { hashHandlerWorkflows } from "./hashHandlerWorkflows.ts";
|
|
7
|
+
|
|
8
|
+
export function getWorkflows(extensionConfig: ExtensionConfig): Workflow[] {
|
|
9
|
+
return [
|
|
10
|
+
...baseWorkflows,
|
|
11
|
+
...hashHandlerWorkflows,
|
|
12
|
+
|
|
13
|
+
// TODO: think about putting the below workflows above at the beggining
|
|
14
|
+
// this will give us the ability to specify which roles have the ability to login, register etc
|
|
15
|
+
|
|
16
|
+
// workflows that handles policies
|
|
17
|
+
...meAliasWorkflows,
|
|
18
|
+
...getPoliciesWorkflows(extensionConfig),
|
|
19
|
+
// TODO: record users activity feed
|
|
20
|
+
// {
|
|
21
|
+
// name: "auth_activityFeedCreateWorkflow",
|
|
22
|
+
// eventName: "core.store.createOne",
|
|
23
|
+
// handler: async (input) => {
|
|
24
|
+
// // TODO: insert a activity feed in here
|
|
25
|
+
// return input;
|
|
26
|
+
// },
|
|
27
|
+
// },
|
|
28
|
+
];
|
|
29
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { EventContext, Workflow } from "@lobb-js/core";
|
|
2
|
+
import type { Context } from "hono";
|
|
3
|
+
import type { User } from "../config/extensionConfigSchema.ts";
|
|
4
|
+
|
|
5
|
+
function resolveMeAlias(input: any, ctx: EventContext): number {
|
|
6
|
+
if (input.collectionName === "auth_users" && input.id === "me") {
|
|
7
|
+
const context = input.context as Context;
|
|
8
|
+
const currentUser = context.get("auth_user") as User | undefined;
|
|
9
|
+
|
|
10
|
+
if (!currentUser) {
|
|
11
|
+
throw new ctx.LobbError({
|
|
12
|
+
code: "BAD_REQUEST",
|
|
13
|
+
message: "You need to provide an access token.",
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return currentUser.id;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return input.id;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export const meAliasWorkflows: Workflow[] = [
|
|
24
|
+
{
|
|
25
|
+
name: "auth_userFindOneMeAlias",
|
|
26
|
+
eventName: "core.store.preFindOne",
|
|
27
|
+
handler: async (input, ctx) => {
|
|
28
|
+
input.id = resolveMeAlias(input, ctx);
|
|
29
|
+
return input;
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
name: "auth_userPreUpdateMeAlias",
|
|
34
|
+
eventName: "core.store.preUpdateOne",
|
|
35
|
+
handler: async (input, ctx) => {
|
|
36
|
+
input.id = resolveMeAlias(input, ctx);
|
|
37
|
+
return input;
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
name: "auth_userPreDeleteMeAlias",
|
|
42
|
+
eventName: "core.store.preDeleteOne",
|
|
43
|
+
handler: async (input, ctx) => {
|
|
44
|
+
input.id = resolveMeAlias(input, ctx);
|
|
45
|
+
return input;
|
|
46
|
+
},
|
|
47
|
+
},
|
|
48
|
+
];
|