-
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 #49 from Mango-Entertainment/zod-integration
Zod integration
- Loading branch information
Showing
10 changed files
with
336 additions
and
94 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { createServerClient, type CookieOptions } from '@supabase/ssr' | ||
import { type EmailOtpType } from '@supabase/supabase-js' | ||
import { cookies } from 'next/headers' | ||
import { NextRequest, NextResponse } from 'next/server' | ||
|
||
export async function GET(request: NextRequest) { | ||
const {searchParams} = new URL(request.url) | ||
const token_hash = searchParams.get('token_hash') | ||
const type = searchParams.get('type') as EmailOtpType | null | ||
const next = searchParams.get('next') ?? '/' | ||
const redirectTo = request.nextUrl.clone() | ||
redirectTo.pathname = next | ||
|
||
if (token_hash && type) { | ||
const cookieStore = cookies() | ||
const supabase = createServerClient( | ||
process.env.NEXT_PUBLIC_SUPABASE_URL!, | ||
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!, | ||
{ | ||
cookies: { | ||
get(name: string) { | ||
return cookieStore.get(name)?.value | ||
}, | ||
set(name: string, value: string, options: CookieOptions) { | ||
cookieStore.set({ name, value, ...options }) | ||
}, | ||
remove(name: string, options: CookieOptions) { | ||
cookieStore.delete({ name, ...options }) | ||
}, | ||
}, | ||
} | ||
) | ||
|
||
const { error } = await supabase.auth.verifyOtp({ | ||
type, | ||
token_hash, | ||
}) | ||
if (!error) { | ||
return NextResponse.redirect(redirectTo) | ||
} | ||
} | ||
|
||
// return the user to an error page with some instructions | ||
redirectTo.pathname = '/auth/auth-code-error' | ||
return NextResponse.redirect(redirectTo) | ||
} |
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,47 +1,61 @@ | ||
export type Selection = { | ||
id: string | ||
title: string | ||
category: string | ||
year: number | ||
rating: string | ||
isTrending: boolean | ||
isBookmarked: boolean | ||
} | ||
import { z } from 'zod' | ||
|
||
export type TrendingThumbs = { | ||
id: string | ||
selectionId: string | ||
small: string | ||
large: string | ||
} | ||
const User = z.object({ | ||
id: z.string(), | ||
email: z.string().email({ message: "Invalid email address" }), | ||
password: z.string().min(7, { message: 'Must be 7 or more characters long' }), | ||
}) | ||
|
||
export type RegularThumbs = { | ||
id: string | ||
selectionId: string | ||
small: string | ||
medium: string | ||
large: string | ||
} | ||
const Selection = z.object({ | ||
id: z.string(), | ||
title: z.string(), | ||
category: z.string(), | ||
year: z.number(), | ||
rating: z.string(), | ||
isTrending: z.boolean(), | ||
isBookmarked: z.boolean(), | ||
}) | ||
|
||
export type TrendingData = { | ||
id: string | ||
title: string | ||
rating: string | ||
year: number | ||
category: string | ||
is_bookmarked: boolean | ||
large: string | ||
small: string | ||
} | ||
const TrendingThumbs = z.object({ | ||
id: z.string(), | ||
selectionId: z.string(), | ||
small: z.string(), | ||
large: z.string(), | ||
}) | ||
|
||
export type RegularData = { | ||
id: string | ||
title: string | ||
rating: string | ||
year: number | ||
category: string | ||
is_bookmarked: boolean | ||
large: string | ||
small: string | ||
medium: string | ||
} | ||
const RegularThumbs = z.object({ | ||
id: z.string(), | ||
selectionId: z.string(), | ||
small: z.string(), | ||
medium: z.string(), | ||
large: z.string(), | ||
}) | ||
|
||
const TrendingData = z.object({ | ||
id: z.string(), | ||
title: z.string(), | ||
rating: z.string(), | ||
year: z.number(), | ||
category: z.string(), | ||
is_bookmarked: z.boolean(), | ||
large: z.string(), | ||
small: z.string(), | ||
}) | ||
|
||
const RegularData = z.object({ | ||
id: z.string(), | ||
title: z.string(), | ||
rating: z.string(), | ||
year: z.number(), | ||
category: z.string(), | ||
is_bookmarked: z.boolean(), | ||
large: z.string(), | ||
small: z.string(), | ||
medium: z.string(), | ||
}) | ||
|
||
export type Selection = z.infer<typeof Selection> | ||
export type TrendingThumbs = z.infer<typeof TrendingThumbs> | ||
export type RegularThumbs = z.infer<typeof RegularThumbs> | ||
export type TrendingData = z.infer<typeof TrendingData> | ||
export type RegularData = z.infer<typeof RegularData> |
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,27 @@ | ||
'use server' | ||
import { redirect } from 'next/navigation' | ||
import { headers, cookies } from 'next/headers' | ||
import { createClient } from '@/utils/supabase/server' | ||
|
||
|
||
const signUpWithEmail = async ({ email, password }: {email: string, password: string}) => { | ||
const origin = headers().get('origin') | ||
const cookieStore = cookies() | ||
const supabase = createClient(cookieStore) | ||
|
||
const { error } = await supabase.auth.signUp({ | ||
email, | ||
password, | ||
options: { | ||
emailRedirectTo: `${origin}/auth/callback`, | ||
}, | ||
}) | ||
|
||
if (error) { | ||
return redirect('/login?message=Could not authenticate user') | ||
} | ||
|
||
return redirect('/login?message=Check email to continue sign in process') | ||
} | ||
|
||
export { signUpWithEmail } |
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,68 @@ | ||
'use client' | ||
import Link from "next/link" | ||
import Image from "next/image" | ||
import { createClient } from '@/utils/supabase/client' | ||
import { useState } from "react" | ||
|
||
const Auth = () => { | ||
const [user, setUser] = useState('') | ||
const supabase = createClient() | ||
|
||
const subscription = supabase.auth.onAuthStateChange(async (event, session) => { | ||
console.log(event, session) | ||
|
||
if (event === 'INITIAL_SESSION') { | ||
// handle initial session | ||
const { data, error } = await supabase.auth.getSession() | ||
} else if (event === 'SIGNED_IN') { | ||
// handle sign in event | ||
// set user | ||
const { data, error } = await supabase.auth.getUser() | ||
if(!error?.status) { | ||
setUser(data.user?.email || '') | ||
} | ||
} else if (event === 'SIGNED_OUT') { | ||
// handle sign out event | ||
// unset user | ||
setUser('') | ||
} else if (event === 'PASSWORD_RECOVERY') { | ||
// handle password recovery event | ||
// supabase.auth.resetPasswordForEmail() | ||
} else if (event === 'TOKEN_REFRESHED') { | ||
// handle token refreshed event | ||
} else if (event === 'USER_UPDATED') { | ||
// handle user updated event | ||
} | ||
}) | ||
|
||
// call unsubscribe to remove the callback | ||
// subscription.unsubscribe() | ||
|
||
if(user === ''){ | ||
return ( | ||
<div className="bg-entertainment-red text-entertainment-pure-white text-center w-14 h-7 mr-4 rounded-md md:h-8 md:mr-6 lg:mr-0 lg:mb-8 flex align-center justify-center" | ||
> | ||
<Link | ||
href="/login" | ||
className="self-center" | ||
> | ||
Login | ||
</Link> | ||
</div> | ||
) | ||
} | ||
return ( | ||
<div> | ||
<Image | ||
className="w-6 h-6 mr-4 border-2 rounded-full md:w-8 md:h-8 md:mr-6 lg:mr-0 lg:w-10 lg:h-10 lg:mb-8 border-entertainment-pure-white" | ||
src="/image-avatar.png" | ||
onClick={() => supabase.auth.signOut()} | ||
alt="icon" | ||
width={80} | ||
height={80} | ||
/> | ||
</div> | ||
) | ||
} | ||
|
||
export default Auth |
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
Oops, something went wrong.