redirect on login now works
This commit is contained in:
		
							parent
							
								
									8a04297768
								
							
						
					
					
						commit
						9e807f8824
					
				| 
						 | 
				
			
			@ -6,28 +6,21 @@ import { loginSchema, LoginSchema } from 'app/login/schema';
 | 
			
		|||
import { NextResponse } from 'next/server';
 | 
			
		||||
 | 
			
		||||
export async function getJWTSecretKey<Uint8Array>() {
 | 
			
		||||
	console.log('getJWTSecretKey called!')
 | 
			
		||||
	const secret = process.env.JWT_SECRET
 | 
			
		||||
	console.log('secret: ' + secret)
 | 
			
		||||
	if (!secret) throw new Error("There is no JWT secret key")
 | 
			
		||||
	console.log('encoding...')
 | 
			
		||||
	try {
 | 
			
		||||
		const enc: Uint8Array = new TextEncoder().encode(secret)
 | 
			
		||||
		console.log('enc')
 | 
			
		||||
		return enc
 | 
			
		||||
	} catch (error) {
 | 
			
		||||
		console.error('aw shit: ' + error.message)
 | 
			
		||||
		throw new Error("failed to getJWTSecretKey", error.message)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export async function verifyJwt(token: string): Promise<JWTPayload | null> {
 | 
			
		||||
	console.log('verifyJwt called for token: ' + token)
 | 
			
		||||
	const key = await getJWTSecretKey()
 | 
			
		||||
	console.log('key: ' + key)
 | 
			
		||||
	const { payload } = await jwtVerify(token, key)
 | 
			
		||||
	console.log('payload: ' + payload)
 | 
			
		||||
	return payload
 | 
			
		||||
	try {
 | 
			
		||||
		const key = await getJWTSecretKey()
 | 
			
		||||
		const { payload } = await jwtVerify(token, key)
 | 
			
		||||
		return payload
 | 
			
		||||
	} catch {
 | 
			
		||||
		return null
 | 
			
		||||
	}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -9,22 +9,22 @@ import { Button } from "@/components/ui/button";
 | 
			
		|||
import { redirect } from "next/navigation";
 | 
			
		||||
import { loginSchema } from "./schema";
 | 
			
		||||
import { useRouter } from "next/navigation";
 | 
			
		||||
 | 
			
		||||
import { useSearchParams } from "next/navigation";
 | 
			
		||||
import revalidate from "./revalidate";
 | 
			
		||||
import { useState } from "react";
 | 
			
		||||
import Link from "next/link";
 | 
			
		||||
 | 
			
		||||
export default function LoginForm() {
 | 
			
		||||
	const router = useRouter()
 | 
			
		||||
	const searchParams = useSearchParams()
 | 
			
		||||
	const redirect = searchParams.get("from")
 | 
			
		||||
	const form = useForm<z.infer<typeof loginSchema>>({
 | 
			
		||||
		resolver: zodResolver(loginSchema),
 | 
			
		||||
	})
 | 
			
		||||
	const [submitted, setSubmitted] = useState(false)
 | 
			
		||||
 | 
			
		||||
	const onSubmit = form.handleSubmit(async (data) => {
 | 
			
		||||
		// const res = await login(data)
 | 
			
		||||
		// if (res?.error) {
 | 
			
		||||
		// 	toast({ title: "Whoops!", description: res.error })
 | 
			
		||||
		// 	form.reset()
 | 
			
		||||
		// } else {
 | 
			
		||||
		// 	toast({ title: "login successful" })
 | 
			
		||||
		// }
 | 
			
		||||
	const onSubmit = form.handleSubmit(async (data, event) => {
 | 
			
		||||
		event.preventDefault()
 | 
			
		||||
		const res = await fetch('/api/auth/login', {
 | 
			
		||||
			method: 'POST',
 | 
			
		||||
			headers: {
 | 
			
		||||
| 
						 | 
				
			
			@ -34,7 +34,10 @@ export default function LoginForm() {
 | 
			
		|||
		})
 | 
			
		||||
		if (res.status === 200) {
 | 
			
		||||
			toast({ title: "login successful!" })
 | 
			
		||||
			router.push('/submission')
 | 
			
		||||
			setSubmitted(true)
 | 
			
		||||
			await revalidate(redirect)
 | 
			
		||||
			router.refresh()
 | 
			
		||||
			router.push(redirect)
 | 
			
		||||
		} else {
 | 
			
		||||
			toast({ title: "login failed!" })
 | 
			
		||||
		}
 | 
			
		||||
| 
						 | 
				
			
			@ -42,37 +45,41 @@ export default function LoginForm() {
 | 
			
		|||
 | 
			
		||||
 | 
			
		||||
	return (
 | 
			
		||||
		<Form {...form}>
 | 
			
		||||
			<form onSubmit={onSubmit}>
 | 
			
		||||
				<FormField
 | 
			
		||||
					control={form.control}
 | 
			
		||||
					name="email"
 | 
			
		||||
					render={({ field }) => (
 | 
			
		||||
						<FormItem>
 | 
			
		||||
							<FormLabel>Email Address</FormLabel>
 | 
			
		||||
							<FormControl>
 | 
			
		||||
								<Input placeholder="email goes here" {...field} />
 | 
			
		||||
							</FormControl>
 | 
			
		||||
							<FormMessage />
 | 
			
		||||
						</FormItem>
 | 
			
		||||
					)}
 | 
			
		||||
				></FormField>
 | 
			
		||||
				<FormField
 | 
			
		||||
					control={form.control}
 | 
			
		||||
					name="password"
 | 
			
		||||
					render={({ field }) => (
 | 
			
		||||
						<FormItem>
 | 
			
		||||
							<FormLabel>Password</FormLabel>
 | 
			
		||||
							<FormControl>
 | 
			
		||||
								<Input placeholder="password goes here" type="password"{...field} />
 | 
			
		||||
							</FormControl>
 | 
			
		||||
							<FormMessage />
 | 
			
		||||
						</FormItem>
 | 
			
		||||
					)}
 | 
			
		||||
				></FormField>
 | 
			
		||||
				<Button type="submit">SUBMIT</Button>
 | 
			
		||||
			</form>
 | 
			
		||||
		</Form>
 | 
			
		||||
		<>
 | 
			
		||||
			{submitted ? <p>Logging in...</p> :
 | 
			
		||||
				<Form {...form}>
 | 
			
		||||
					<form onSubmit={onSubmit}>
 | 
			
		||||
						<FormField
 | 
			
		||||
							control={form.control}
 | 
			
		||||
							name="email"
 | 
			
		||||
							render={({ field }) => (
 | 
			
		||||
								<FormItem>
 | 
			
		||||
									<FormLabel>Email Address</FormLabel>
 | 
			
		||||
									<FormControl>
 | 
			
		||||
										<Input placeholder="email goes here" {...field} />
 | 
			
		||||
									</FormControl>
 | 
			
		||||
									<FormMessage />
 | 
			
		||||
								</FormItem>
 | 
			
		||||
							)}
 | 
			
		||||
						></FormField>
 | 
			
		||||
						<FormField
 | 
			
		||||
							control={form.control}
 | 
			
		||||
							name="password"
 | 
			
		||||
							render={({ field }) => (
 | 
			
		||||
								<FormItem>
 | 
			
		||||
									<FormLabel>Password</FormLabel>
 | 
			
		||||
									<FormControl>
 | 
			
		||||
										<Input placeholder="password goes here" type="password"{...field} />
 | 
			
		||||
									</FormControl>
 | 
			
		||||
									<FormMessage />
 | 
			
		||||
								</FormItem>
 | 
			
		||||
							)}
 | 
			
		||||
						></FormField>
 | 
			
		||||
						<Button type="submit">SUBMIT</Button>
 | 
			
		||||
					</form>
 | 
			
		||||
				</Form>
 | 
			
		||||
			}
 | 
			
		||||
		</>
 | 
			
		||||
 | 
			
		||||
	)
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -0,0 +1,11 @@
 | 
			
		|||
"use server"
 | 
			
		||||
import { revalidatePath } from "next/cache"
 | 
			
		||||
export default async function revalidate(path: string) {
 | 
			
		||||
	try {
 | 
			
		||||
		revalidatePath(path)
 | 
			
		||||
		return true
 | 
			
		||||
	} catch (error) {
 | 
			
		||||
		console.error(error)
 | 
			
		||||
		return false
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -24,7 +24,7 @@ export default function GenrePickerInputCell(props: CellContext<any, any>) {
 | 
			
		|||
  const genres = props.table.options.meta.genres
 | 
			
		||||
  const [isActive, setIsActive] = useState(false)
 | 
			
		||||
 | 
			
		||||
  async function onSubmit({ genres }: { genres: number[] }) {
 | 
			
		||||
  async function onSubmit({ genres }: { genres: number[] }, event: Event) {
 | 
			
		||||
    event.preventDefault()
 | 
			
		||||
    const genresArray = genres.map((e) => { return { id: e } })
 | 
			
		||||
    console.log(`genres: ${genres}, genresArray: ${JSON.stringify(genresArray)}`)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -17,11 +17,13 @@ export default async function(request: NextRequest) {
 | 
			
		|||
	// const url = `${process.env.NEXT_PUBLIC_BASE_URL}/login?redirect=${request.nextUrl.pathname + request.nextUrl.search}`
 | 
			
		||||
	const url = request.nextUrl.clone()
 | 
			
		||||
	url.pathname = "/login"
 | 
			
		||||
	url.searchParams.set('from', request.nextUrl.pathname)
 | 
			
		||||
	if (protectedRoutes.some(pattern => matchesWildcard(request.nextUrl.pathname, pattern))) {
 | 
			
		||||
		const token = request.cookies.get('token')
 | 
			
		||||
		//NOTE - may need to add logic to return 401 for api routes
 | 
			
		||||
 | 
			
		||||
		if (!token) {
 | 
			
		||||
			console.log("there is no jwt")
 | 
			
		||||
			return NextResponse.redirect(url)
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -34,12 +36,14 @@ export default async function(request: NextRequest) {
 | 
			
		|||
				request.cookies.delete('token')
 | 
			
		||||
				return NextResponse.redirect(url)
 | 
			
		||||
			}
 | 
			
		||||
		} catch {
 | 
			
		||||
		} catch (error) {
 | 
			
		||||
			//delete token (failsafe)
 | 
			
		||||
			console.error("failed to very jwt", error.message)
 | 
			
		||||
			request.cookies.delete('token')
 | 
			
		||||
			return NextResponse.redirect(url)
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		//TODO - TEST THIS BECAUSE IT PROBABLY DOESN'T WORK
 | 
			
		||||
		//redirect from login if already logged in
 | 
			
		||||
		let redirectToApp = false
 | 
			
		||||
		if (request.nextUrl.pathname === "/login") {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue