-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from kcoderhtml/dev
v0.2.0
- Loading branch information
Showing
12 changed files
with
419 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,101 +1,107 @@ | ||
import Slack from "@auth/core/providers/slack"; | ||
import { defineConfig } from "auth-astro"; | ||
import { db, like, User, Organization } from "astro:db"; | ||
import { db, like, and, User, Organization } from "astro:db"; | ||
|
||
export default defineConfig({ | ||
providers: [ | ||
Slack({ | ||
clientId: import.meta.env.SLACK_CLIENT_ID, | ||
clientSecret: import.meta.env.SLACK_CLIENT_SECRET, | ||
checks: ["pkce", "nonce"], | ||
async profile(profile) { | ||
const role = await db | ||
.select() | ||
.from(User) | ||
.where(like(User.userId, profile["https://slack.com/user_id"])); | ||
providers: [ | ||
Slack({ | ||
clientId: import.meta.env.SLACK_CLIENT_ID, | ||
clientSecret: import.meta.env.SLACK_CLIENT_SECRET, | ||
checks: ["pkce", "nonce"], | ||
async profile(profile) { | ||
profile["https://slack.com/team_id"] = | ||
"slack-" + profile["https://slack.com/team_id"]; | ||
|
||
if (role.length === 0) { | ||
const users = await db | ||
.select() | ||
.from(User) | ||
.where(like(User.team, profile["https://slack.com/team_id"])); | ||
const role = await db | ||
.select() | ||
.from(User) | ||
.where( | ||
and(like(User.userId, profile["https://slack.com/user_id"])), | ||
like(User.team, profile["https://slack.com/team_id"]) | ||
); | ||
|
||
const organizations = await db | ||
.select() | ||
.from(Organization) | ||
.where( | ||
like(Organization.team, profile["https://slack.com/team_id"]), | ||
); | ||
if (role.length === 0) { | ||
const users = await db | ||
.select() | ||
.from(User) | ||
.where(like(User.team, profile["https://slack.com/team_id"])); | ||
|
||
// check if the user is part of an organization in the db by checking if the team id is the same | ||
if ( | ||
organizations | ||
.map((org) => org.team) | ||
.includes(profile["https://slack.com/team_id"]) | ||
) { | ||
if (users.length === 0) { | ||
await db.insert(User).values({ | ||
userId: profile["https://slack.com/user_id"], | ||
name: profile.name, | ||
email: profile.email, | ||
image: profile.picture, | ||
team: profile["https://slack.com/team_id"], | ||
role: "admin", | ||
}); | ||
const organizations = await db | ||
.select() | ||
.from(Organization) | ||
.where( | ||
like(Organization.team, profile["https://slack.com/team_id"]) | ||
); | ||
|
||
role[0] = { role: "admin" }; | ||
} else { | ||
await db.insert(User).values({ | ||
userId: profile["https://slack.com/user_id"], | ||
name: profile.name, | ||
email: profile.email, | ||
image: profile.picture, | ||
team: profile["https://slack.com/team_id"], | ||
role: "user", | ||
}); | ||
// check if the user is part of an organization in the db by checking if the team id is the same | ||
if ( | ||
organizations | ||
.map((org) => org.team) | ||
.includes(profile["https://slack.com/team_id"]) | ||
) { | ||
if (users.length === 0) { | ||
await db.insert(User).values({ | ||
userId: profile["https://slack.com/user_id"], | ||
name: profile.name, | ||
email: profile.email, | ||
image: profile.picture, | ||
team: profile["https://slack.com/team_id"], | ||
role: "admin", | ||
}); | ||
|
||
role[0] = { role: "user" }; | ||
} | ||
} else { | ||
role[0] = { role: "guest" }; | ||
} | ||
} | ||
role[0] = { role: "admin" }; | ||
} else { | ||
await db.insert(User).values({ | ||
userId: profile["https://slack.com/user_id"], | ||
name: profile.name, | ||
email: profile.email, | ||
image: profile.picture, | ||
team: profile["https://slack.com/team_id"], | ||
role: "user", | ||
}); | ||
|
||
return { | ||
id: profile["https://slack.com/user_id"], | ||
name: profile.name, | ||
email: profile.email, | ||
image: profile.picture, | ||
team: profile["https://slack.com/team_id"], | ||
teamName: profile["https://slack.com/team_name"], | ||
teamImage: profile["https://slack.com/team_image_230"], | ||
role: role[0].role || "user", | ||
}; | ||
}, | ||
}), | ||
], | ||
callbacks: { | ||
jwt({ token, user }) { | ||
if (user) { | ||
// User is available during sign-in | ||
token.team = user.team; | ||
token.teamName = user.teamName; | ||
token.teamImage = user.teamImage; | ||
token.role = user.role; | ||
token.id = user.id; | ||
} | ||
return token; | ||
}, | ||
session({ session, token }) { | ||
if (token) { | ||
// Token is available during sign-in | ||
session.team = token.team; | ||
session.teamName = token.teamName; | ||
session.teamImage = token.teamImage; | ||
session.user.role = token.role; | ||
session.user.id = token.id; | ||
} | ||
return session; | ||
}, | ||
}, | ||
role[0] = { role: "user" }; | ||
} | ||
} else { | ||
role[0] = { role: "guest" }; | ||
} | ||
} | ||
|
||
return { | ||
id: profile["https://slack.com/user_id"], | ||
name: profile.name, | ||
email: profile.email, | ||
image: profile.picture, | ||
team: profile["https://slack.com/team_id"], | ||
teamName: profile["https://slack.com/team_name"], | ||
teamImage: profile["https://slack.com/team_image_230"], | ||
role: role[0].role || "user", | ||
}; | ||
}, | ||
}), | ||
], | ||
callbacks: { | ||
jwt({ token, user }) { | ||
if (user) { | ||
// User is available during sign-in | ||
token.team = user.team; | ||
token.teamName = user.teamName; | ||
token.teamImage = user.teamImage; | ||
token.role = user.role; | ||
token.id = user.id; | ||
} | ||
return token; | ||
}, | ||
session({ session, token }) { | ||
if (token) { | ||
// Token is available during sign-in | ||
session.team = token.team; | ||
session.teamName = token.teamName; | ||
session.teamImage = token.teamImage; | ||
session.user.role = token.role; | ||
session.user.id = token.id; | ||
} | ||
return session; | ||
}, | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import type { APIRoute } from "astro" | ||
import { db, User, Organization, Event } from "astro:db"; | ||
|
||
export const POST: APIRoute = async ({ request }) => { | ||
// get authorization header | ||
const auth = request.headers.get("Authorization") | ||
|
||
if (auth === process.env.API_SECRET) { | ||
const events = await db.select().from(Event) | ||
const users = await db.select().from(User) | ||
const organizations = await db.select().from(Organization) | ||
|
||
console.log(`Found ${events.length} events and ${users.length} users`) | ||
|
||
const eventsHappeningToday = events.filter((e) => { | ||
const eventDate = new Date(e.date) | ||
const now = new Date() | ||
const diff = eventDate.getTime() - now.getTime() | ||
const diffHours = diff / (1000 * 60 * 60) | ||
return diffHours < 24 && diffHours > 0 | ||
}) | ||
|
||
return new Response(JSON.stringify({ | ||
ok: true, eventsHappeningToday: eventsHappeningToday, users: users, organizations: organizations | ||
}), { status: 200 }) | ||
} else { | ||
return new Response(JSON.stringify({ | ||
ok: false, error: "Unauthorized" | ||
}), { status: 401 }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.