Skip to content

Commit

Permalink
astro auth
Browse files Browse the repository at this point in the history
  • Loading branch information
haffi96 committed Sep 17, 2023
1 parent b833865 commit dc5c5dc
Show file tree
Hide file tree
Showing 14 changed files with 520 additions and 64 deletions.
67 changes: 51 additions & 16 deletions aws/backend/db/query.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { db } from "./session"
import { allBlogs, blogPosts, userBlogs, userPosts , users } from "./schema"
import { eq, desc, sql, gt} from "drizzle-orm"
import { allBlogs, blogPosts, userBlogs, userPosts, users } from "./schema"
import { eq, desc, sql, gt } from "drizzle-orm"


export type newBlogEntry = typeof allBlogs.$inferInsert;
Expand All @@ -12,16 +12,18 @@ export const fetchUserById = async (userId: number) => {
return await db.select({ userUuid: users.user_uuid }).from(users).where(eq(users.id, userId))
}

export const fetchUserSubscribedBlogs = async (userId: number) => {
const sq = db.select({ blogId: userBlogs.blog_id }).from(userBlogs).where(eq(userBlogs.user_id, userId)).as("sq")
export const fetchUserSubscribedBlogs = async (userEmail: string) => {
const userQuery = db.select({ userId: users.id }).from(users).where(eq(users.email, userEmail)).as("userQuery")

const sq = db.select({ blog_id: userBlogs.blog_id }).from(userBlogs).innerJoin(userQuery, eq(userBlogs.user_id, userQuery.userId)).as("sq")

return await db.select({
blogId: allBlogs.id,
blogLink: allBlogs.link,
companyName: allBlogs.companyName
})
.from(allBlogs)
.innerJoin(sq, eq(allBlogs.id, sq.blogId))
.innerJoin(sq, eq(allBlogs.id, sq.blog_id))
}


Expand All @@ -34,8 +36,35 @@ export const getBlogByCompanyName = async (blogName: string): Promise<blogEntry[
return await db.select().from(allBlogs).where(eq(allBlogs.companyName, blogName))
}

export const fetchAllBlogs = async () => {
return await db.select({
blogId: allBlogs.id,
blogLink: allBlogs.link,
companyName: allBlogs.companyName
}).from(allBlogs)
}


// BlogPosts table
export const fetchAllPosts = async ({ offset, limit }: { offset: number, limit: number }) => {

return await db.select({
postId: blogPosts.id,
postUuid: blogPosts.post_uuid,
title: blogPosts.title,
link: blogPosts.link,
author: blogPosts.author,
publishedDate: blogPosts.publishedDate,
blogId: blogPosts.blog_id,
companyName: allBlogs.companyName,
})
.from(blogPosts)
.innerJoin(allBlogs, eq(allBlogs.id, blogPosts.blog_id))
.orderBy(desc(blogPosts.publishedDate))
.offset(offset)
.limit(limit)
}

export const createBlogPostEntry = async (newBlogPostParams: newBlogPostEntry) => {
return await db
.insert(blogPosts)
Expand All @@ -61,13 +90,15 @@ export const fetchPostById = async (postId: number) => {

// UserBlogs table
interface QueryAllPostsParams {
userId: number;
userEmail: string;
offset: number;
limit: number;
}

export const fetchAllPostsForUser = async ({ userId, offset, limit }: QueryAllPostsParams) => {
const sq = db.select().from(userBlogs).where(eq(userBlogs.user_id, userId)).as("sq")
export const fetchAllPostsForUser = async ({ userEmail, offset, limit }: QueryAllPostsParams) => {
const userQuery = db.select({ userId: users.id }).from(users).where(eq(users.email, userEmail)).as("userQuery")

const sq = db.select({ blog_id: userBlogs.blog_id }).from(userBlogs).innerJoin(userQuery, eq(userBlogs.user_id, userQuery.userId)).as("sq")

return await db.select({
postId: blogPosts.id,
Expand All @@ -88,13 +119,13 @@ export const fetchAllPostsForUser = async ({ userId, offset, limit }: QueryAllPo
}

export const fetchUsersForBlog = async (blogId: number | null) => {
return await db.select({userId: userBlogs.user_id}).from(userBlogs).where(eq(userBlogs.blog_id, blogId!))
return await db.select({ userId: userBlogs.user_id }).from(userBlogs).where(eq(userBlogs.blog_id, blogId!))
}


// UserPosts table
export const createUserPostEntry = async (userId: number, postId: number) => {
return await db.insert(userPosts).values({user_id: userId, post_id: postId})
return await db.insert(userPosts).values({ user_id: userId, post_id: postId })
}


Expand Down Expand Up @@ -146,14 +177,18 @@ export const fetchTodaysUsersToNotify = async () => {
// console.log(users);
// });

// fetchAllPostsForUser({ userId: 1, offset: 0, limit: 10 }).then((posts) => {
// console.log(posts);
// });
// fetchAllPostsForUser({ userEmail: "haff@test.com", offset: 0, limit: 10 }).then((posts) => {
// console.log(posts)
// })

// fetchUserSubscribedBlogs("haff@test.com").then((res) => {
// console.log(res)
// })

// fetchUserSubscribedBlogs(1).then((res) => {
// console.log(res);
// });

// fetchAllBlogs().then((res) => {
// console.log(res)
// })

// getBlogByCompanyName('Cloudflare').then((res) => {
// console.log(res);
Expand Down
9 changes: 5 additions & 4 deletions aws/backend/lambda/GetSubscribedBlogs.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { APIGatewayProxyEvent, APIGatewayProxyResult } from "aws-lambda"
import { fetchUserSubscribedBlogs } from "../db/query"
import { fetchUserSubscribedBlogs, fetchAllBlogs } from "../db/query"

export const handler = async (event: APIGatewayProxyEvent): Promise<APIGatewayProxyResult> => {
const params = event.queryStringParameters
const userId = params?.userId ? parseInt(params.userId) : null
const userEmail = params?.userEmail

try {
const subscribedBlogs = await fetchUserSubscribedBlogs(userId!)
console.log(subscribedBlogs)
const subscribedBlogs = userEmail
? await fetchUserSubscribedBlogs(userEmail)
: await fetchAllBlogs()
return {
statusCode: 200,
body: JSON.stringify(subscribedBlogs),
Expand Down
9 changes: 6 additions & 3 deletions aws/backend/lambda/Posts.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { APIGatewayProxyEvent, APIGatewayProxyResult } from "aws-lambda"
import { fetchAllPostsForUser } from "../db/query"
import { fetchAllPostsForUser, fetchAllPosts } from "../db/query"

export const handler = async (event: APIGatewayProxyEvent): Promise<APIGatewayProxyResult> => {
const params = event.queryStringParameters
const userId = params?.userId ? parseInt(params.userId) : null
const userEmail = params?.userEmail
const offset = params?.offset ? parseInt(params.offset) : 0
const limit = params?.limit ? parseInt(params.limit) : 10

try {
const posts = await fetchAllPostsForUser({ userId: userId!, offset, limit })

const posts = userEmail
? await fetchAllPostsForUser({ userEmail, offset, limit })
: await fetchAllPosts({ offset: 0, limit: 30 })
return {
statusCode: 200,
body: JSON.stringify(posts),
Expand Down
1 change: 0 additions & 1 deletion aws/events/apiGatewayHttp.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
"Header2": "value1,value2"
},
"queryStringParameters": {
"userId": 1,
"limit": 10,
"offset": 0
},
Expand Down
9 changes: 5 additions & 4 deletions ui/astro.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ import tailwind from "@astrojs/tailwind";
import preact from "@astrojs/preact";
import vercel from "@astrojs/vercel/serverless";

import auth from "auth-astro";

// https://astro.build/config
export default defineConfig({
output: 'server',
adapter: vercel(),
server: {
port: 4321
},
integrations: [
tailwind(),
preact({ compat: true })
]
integrations: [tailwind(), preact({
compat: true
}), auth()]
});
10 changes: 10 additions & 0 deletions ui/auth.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import Google from '@auth/core/providers/google'

export default {
providers: [
Google({
clientId: import.meta.env.GOOGLE_CLIENT_ID,
clientSecret: import.meta.env.GOOGLE_CLIENT_SECRET,
}),
]
}
2 changes: 2 additions & 0 deletions ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
"@astrojs/preact": "^3.0.0",
"@astrojs/tailwind": "^5.0.0",
"@astrojs/vercel": "^5.0.0",
"@auth/core": "^0.5.1",
"astro": "^3.0.7",
"auth-astro": "^3.0.1",
"preact": "^10.6.5",
"tailwindcss": "^3.3.3",
"typescript": "^5.2.2"
Expand Down
Loading

0 comments on commit dc5c5dc

Please sign in to comment.