@@ -205,4 +205,90 @@ export const config = {
205205- this file will contain all types of routes
206206
20720744 . Edit middleware.ts file
208- // cuurently here : 2:32:14
208+
209+ - add routes condition in ` middleware.ts ` file
210+
211+ 45 . Do valdiations in ` auth.config.ts ` file
212+
213+ 46 . Edit ` auth.ts ` file
214+
215+ ```
216+ export const {
217+ handlers: { GET, POST },
218+ auth,
219+ signIn,
220+ signOut,
221+ } = NextAuth({
222+ adapter: PrismaAdapter(db), // prisma adapter is supported on non edge
223+ session: { strategy: "jwt" },
224+ ...authConfig,
225+ });
226+
227+ ```
228+
229+ 47 . Implement functionality in ` login.ts ` file
230+
231+ ```
232+ "use server"; // necessary in every auth action
233+
234+ import * as z from "zod";
235+ import { LoginSchema } from "@/schema";
236+ import { signIn } from "@/auth";
237+ import { DEFAULT_LOGIN_REDIRECT } from "@/route";
238+ import { AuthError } from "next-auth";
239+
240+ export const Login = async (values: z.infer<typeof LoginSchema>) => {
241+ const validatedFields = LoginSchema.safeParse(values); // valdiating the input values
242+ if (!validatedFields.success) {
243+ return { error: "Invalid fields! " };
244+ }
245+ const { email, password } = validatedFields.data;
246+ try {
247+ await signIn("credentials", {
248+ email,
249+ password,
250+ redirectTo: DEFAULT_LOGIN_REDIRECT,
251+ });
252+ } catch (error) {
253+ if (error instanceof AuthError) {
254+ switch (error.type) {
255+ case "CredentialsSignin":
256+ return { error: "Invalid credentials!" };
257+ default:
258+ return { error: "Something went wrong!" };
259+ }
260+ }
261+ throw error;
262+ }
263+ };
264+
265+ ```
266+
267+ 48 . Go to settings page and add logout button, and its functionality in ` settings.tsx ` file
268+
269+ ```
270+ import { auth, signOut } from '@/auth'
271+ import React from 'react'
272+
273+ const Settings = async () => {
274+ const user = await auth()
275+ return (
276+ <div>
277+ {JSON.stringify(user)}
278+ <form action={async () => {
279+ 'use server'
280+ await signOut()
281+ }}>
282+ <button type='submit'>
283+ Singout
284+ </button>
285+ </form>
286+ </div>
287+ )
288+ }
289+
290+ export default Settings
291+
292+ ```
293+
294+ #### Login/Logout completed succesfully
0 commit comments