Skip to content

Commit

Permalink
prettier update
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben0189 committed Oct 5, 2024
1 parent d9b5d4d commit 245feb4
Show file tree
Hide file tree
Showing 15 changed files with 302 additions and 272 deletions.
162 changes: 80 additions & 82 deletions blotztask-ui/src/app/api/auth/[...nextauth]/options.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NextAuthOptions } from "next-auth";
import CredentialsProvider from "next-auth/providers/credentials";
import { cookies } from "next/headers";
import { NextAuthOptions } from 'next-auth';
import CredentialsProvider from 'next-auth/providers/credentials';
import { cookies } from 'next/headers';

interface Credentials {
email: string;
Expand All @@ -15,96 +15,94 @@ interface LoginApiResponse {
}

export const authOptions: NextAuthOptions = {
// Configure one or more authentication providers
providers: [
CredentialsProvider({
// The name to display on the sign in form (e.g. "Sign in with...")
name: "Credentials",
// `credentials` is used to generate a form on the sign in page.
// You can specify which fields should be submitted, by adding keys to the `credentials` object.
// e.g. domain, username, password, 2FA token, etc.
// You can pass any HTML attribute to the <input> tag through the object.
credentials: {
email: { label: "Email", type: "text", placeholder: "your email" },
password: { label: "Password", type: "password" }
},

async authorize(credentials: Credentials) {
const { email, password } = credentials;
// Configure one or more authentication providers
providers: [
CredentialsProvider({
// The name to display on the sign in form (e.g. "Sign in with...")
name: 'Credentials',
// `credentials` is used to generate a form on the sign in page.
// You can specify which fields should be submitted, by adding keys to the `credentials` object.
// e.g. domain, username, password, 2FA token, etc.
// You can pass any HTML attribute to the <input> tag through the object.
credentials: {
email: { label: 'Email', type: 'text', placeholder: 'your email' },
password: { label: 'Password', type: 'password' },
},


try {
//TODO : Remove reject unauthorized set to false
if (process.env.NODE_ENV !== 'production') {
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
}
//TODO :Also fix the fetch url for login in prod
const response = await fetch(`${process.env.NEXT_PUBLIC_API_BASE_URL}/login/`, {
async authorize(credentials: Credentials) {
const { email, password } = credentials;

try {
//TODO : Remove reject unauthorized set to false
if (process.env.NODE_ENV !== 'production') {
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
}
//TODO :Also fix the fetch url for login in prod
const response = await fetch(
`${process.env.NEXT_PUBLIC_API_BASE_URL}/login/`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json'
'Content-Type': 'application/json',
},
body: JSON.stringify({ email, password })
});

if (!response.ok) {
console.error('Failed to authenticate:', response);
return null;
body: JSON.stringify({ email, password }),
}

const data: LoginApiResponse = await response.json();
);

cookies().set('authToken', data.accessToken, {
secure: process.env.NODE_ENV === 'production',
sameSite: 'strict'
});

if (data.accessToken) {
return {
id: email || 'placeholder-id',
email: email,
accessToken: data.accessToken, // Include the access token here
refreshToken: data.refreshToken,
expiresIn: data.expiresIn,
};
} else {
console.error('Access token not found in response');
return null;
}
} catch (error) {
console.error('Unhandled error:', error);
if (!response.ok) {
console.error('Failed to authenticate:', response);
return null;
}

}
})
],
secret: process.env.NEXTAUTH_SECRET,
callbacks: {
async signIn({ user, account }) {
if (user?.access_token) {
account.access_token = user?.access_token as string

const data: LoginApiResponse = await response.json();

cookies().set('authToken', data.accessToken, {
secure: process.env.NODE_ENV === 'production',
sameSite: 'strict',
});

if (data.accessToken) {
return {
id: email || 'placeholder-id',
email: email,
accessToken: data.accessToken, // Include the access token here
refreshToken: data.refreshToken,
expiresIn: data.expiresIn,
};
} else {
console.error('Access token not found in response');
return null;
}
} catch (error) {
console.error('Unhandled error:', error);
return null;
}
if (user?.refresh_token) {
account.refresh_token = user?.refresh_token as string
}

return true
},
async jwt({ token, session }) {
console.log('jwt callback', token, session);
return token;
},
async session({ session, token, user }) {
console.log('session callback', session, token, user);


return session
}),
],
secret: process.env.NEXTAUTH_SECRET,
callbacks: {
async signIn({ user, account }) {
if (user?.access_token) {
account.access_token = user?.access_token as string;
}
if (user?.refresh_token) {
account.refresh_token = user?.refresh_token as string;
}

return true;
},
session: {
strategy: "jwt"
async jwt({ token, session }) {
console.log('jwt callback', token, session);
return token;
},
async session({ session, token, user }) {
console.log('session callback', session, token, user);

return session;
},

}
},
session: {
strategy: 'jwt',
},
};
9 changes: 4 additions & 5 deletions blotztask-ui/src/app/api/auth/[...nextauth]/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import NextAuth from "next-auth"
import { authOptions } from "./options"
import NextAuth from 'next-auth';
import { authOptions } from './options';


const handler = NextAuth(authOptions)
export { handler as GET, handler as POST }
const handler = NextAuth(authOptions);
export { handler as GET, handler as POST };
6 changes: 4 additions & 2 deletions blotztask-ui/src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const metadata: Metadata = {
};

export default function RootLayout({
children
children,
}: Readonly<{
children: React.ReactNode;
}>) {
Expand All @@ -38,7 +38,9 @@ export default function RootLayout({
>
<MainNav />
{/* <Navbar /> TODO: Implement navbar to navigate between pages*/}
<section className="container mx-auto px-12 py-4">{children}</section>
<section className="container mx-auto px-12 py-4">
{children}
</section>
</ThemeProvider>
</Provider>
</body>
Expand Down
12 changes: 10 additions & 2 deletions blotztask-ui/src/app/navbar/main-nav.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
'use client';

import { ClientSafeProvider, getProviders, signOut, useSession } from 'next-auth/react';
import {
ClientSafeProvider,
getProviders,
signOut,
useSession,
} from 'next-auth/react';
import Image from 'next/image';
import Link from 'next/link';
import { useEffect, useState } from 'react';

export function MainNav({}: React.HTMLAttributes<HTMLElement>) {
const { data: session } = useSession();
const [providers, setProviders] = useState<Record<string, ClientSafeProvider> | null>(null);
const [providers, setProviders] = useState<Record<
string,
ClientSafeProvider
> | null>(null);

// load the registered nextauth providers, in our case is the credential provider
useEffect(() => {
Expand Down
10 changes: 4 additions & 6 deletions blotztask-ui/src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
"use client"
'use client';
import { H1, H3 } from '@/components/ui/heading-with-anchor';

export default function Home() {
return (

<main className="flex flex-col gap-5 p-12 md:items-center md:p-28">
<H1 className='green_gradient'>Blotz
<span className='blue_gradient text-center'>
Task
</span>
<H1 className="green_gradient">
Blotz
<span className="blue_gradient text-center">Task</span>
</H1>

<H3 className="text-lg font-light text-muted-foreground sm:text-xl">
Expand Down
26 changes: 12 additions & 14 deletions blotztask-ui/src/app/provider.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
"use client";
'use client';

import { Session } from "next-auth";
import { SessionProvider } from "next-auth/react";
import { Session } from 'next-auth';
import { SessionProvider } from 'next-auth/react';

interface ProviderProps {
children: React.ReactNode;
session?: Session | null;
}

const Provider: React.FC<ProviderProps> = ({ children, session }) => (
<SessionProvider session={session}>
{children}
</SessionProvider>
);

export default Provider;
children: React.ReactNode;
session?: Session | null;
}

const Provider: React.FC<ProviderProps> = ({ children, session }) => (
<SessionProvider session={session}>{children}</SessionProvider>
);

export default Provider;
2 changes: 1 addition & 1 deletion blotztask-ui/src/app/signin/LoginPage.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
.submitButton {
width: 100%; /* Full width for button */
padding: 10px;
background-color: #4CAF50; /* Green background */
background-color: #4caf50; /* Green background */
color: white; /* White text */
border: none;
border-radius: 4px; /* Rounded corners */
Expand Down
23 changes: 17 additions & 6 deletions blotztask-ui/src/app/signin/page.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"use client";
'use client';

import { signIn } from 'next-auth/react';
import { useRouter, useSearchParams } from 'next/navigation';
Expand Down Expand Up @@ -27,14 +27,15 @@ const LoginPage = () => {
if (result.error) {
setError(result.error); // Set error if login fails
} else {
router.push(callbackUrl);
router.push(callbackUrl);
}
};

return (
<div className={styles.container}>
<div className={styles.form_container}>
<h1 className={styles.title}>User Login</h1> {/* Title of the login page */}
<h1 className={styles.title}>User Login</h1>{' '}
{/* Title of the login page */}
<form onSubmit={handleSubmit}>
<div className={styles.input_group}>
<label className={styles.label}>Email:</label>
Expand All @@ -58,11 +59,21 @@ const LoginPage = () => {
placeholder="Enter your password"
/>
</div>
{error && <p className={styles.error}>{error}</p>} {/* Display error message if any */}
<button type="submit" className="gradient_green_blue_btn gradient_green_blue_btn:hover">Login</button>
{error && <p className={styles.error}>{error}</p>}{' '}
{/* Display error message if any */}
<button
type="submit"
className="gradient_green_blue_btn gradient_green_blue_btn:hover"
>
Login
</button>
</form>
<p className={styles.registerPrompt}>
Don’t have an account? <a href="/signup" className={styles.registerLink}>Register here</a> {/* Registration link */}
Don’t have an account?{' '}
<a href="/signup" className={styles.registerLink}>
Register here
</a>{' '}
{/* Registration link */}
</p>
</div>
</div>
Expand Down
Loading

0 comments on commit 245feb4

Please sign in to comment.