Skip to content

Commit

Permalink
Merge pull request #23 from kcoderhtml/dev
Browse files Browse the repository at this point in the history
v0.2.0
  • Loading branch information
taciturnaxolotl authored Apr 29, 2024
2 parents a7e0da2 + 21a5093 commit b14230d
Show file tree
Hide file tree
Showing 12 changed files with 419 additions and 147 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ yarn-debug.log*
yarn-error.log*
pnpm-debug.log*


# environment variables
.env
.env.production
Expand All @@ -23,3 +22,6 @@ pnpm-debug.log*

# Local Netlify folder
.netlify

# seed file
db/seed.ts
186 changes: 96 additions & 90 deletions auth.config.mjs
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;
},
},
});
Binary file modified bun.lockb
Binary file not shown.
1 change: 1 addition & 0 deletions db/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const Organization = defineTable({
team: column.text({ primaryKey: true }),
name: column.text(),
image: column.text(),
lastMessageHash: column.text({ optional: true, default: "" }),
},
indexes: {
teamx: { on: ["team"], unique: true },
Expand Down
33 changes: 0 additions & 33 deletions db/seed.ts

This file was deleted.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"prettier": "^3.2.5",
"prettier-plugin-astro": "^0.13.0",
"tailwindcss": "^3.4.2",
"ts-md5": "^1.3.1",
"typescript": "^5.4.3"
},
"devDependencies": {
Expand Down
10 changes: 10 additions & 0 deletions src/components/AuthContainer.astro
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ type ExtendedSession = {
<a href="/settings" class="nav">
Settings
</a>
{
session.user.role == "admin" ? (
<>
/{" "}
<a href="/messages" class="nav">
Messages
</a>
</>
) : null
}
</nav>
</div>
</div>
Expand Down
31 changes: 31 additions & 0 deletions src/pages/api/remind.ts
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 })
}
}
22 changes: 12 additions & 10 deletions src/pages/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,13 @@ import AuthContainer from "../components/AuthContainer.astro";
<tbody>
<tr>
<td>Sign-in with Slack</td>
<td
>Allows you to sign in with slack and auto asigns admins based on
slack admin status.</td
>
<td>Allows you to sign in with Slack!</td>
<td>Fully Functional!</td>
</tr>
<tr>
<td>Team Creation</td>
<td>Allows you to create a team and add team members.</td>
<td
>Fully functional but currently a manual process; working on
transitioning to a auto invite system</td
>
<td>Fully functional!</td>
</tr>
<tr>
<td>Event Scheduling</td>
Expand All @@ -75,12 +69,20 @@ import AuthContainer from "../components/AuthContainer.astro";
<tr>
<td>Team Management</td>
<td>Allows teams to manage their team members and roles.</td>
<td>WIP - Active Progress</td>
<td>Fully Functional</td>
</tr>
<tr>
<td>Team Annoucements</td>
<td
>Allows teams to make announcements to their team members via
email.</td
>
<td>Finished!</td>
</tr>
<tr>
<td>FRC Dashboard Integration</td>
<td>Enable easy adding of team members to your FRC Dashboard!</td>
<td>Next up after team management</td>
<td>WIP</td>
</tr>
</tbody>
</table>
Expand Down
Loading

0 comments on commit b14230d

Please sign in to comment.