Skip to content

Commit

Permalink
Fixed Auth JS
Browse files Browse the repository at this point in the history
  • Loading branch information
KrakenDominguezz committed Jun 13, 2024
1 parent 3d82b8d commit 665781c
Show file tree
Hide file tree
Showing 11 changed files with 781 additions and 129 deletions.
23 changes: 0 additions & 23 deletions saraia/app/api/protected/route.ts

This file was deleted.

2 changes: 1 addition & 1 deletion saraia/app/login/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { auth } from "@/auth/auth";
export default async function Login(){
const session = await auth();
if (session) {
redirect('/');
redirect('/dashboard');
}
return (
<div className="h-screen flex justify-center items-center bg-custom-dark bg-opacity-95">
Expand Down
29 changes: 3 additions & 26 deletions saraia/app/loginform/form.tsx
Original file line number Diff line number Diff line change
@@ -1,44 +1,21 @@
'use client';
import { FormEvent } from "react";
import { db } from "@/lib/db";
import { SignInResponse, signIn } from "next-auth/react";
import { signIn } from "@/auth/auth"
import {Input} from "@/components/ui/input";
import { Label } from "@/components/ui/label"
import { Button } from "@/components/ui/button";
import { useRouter } from "next/navigation";
import { any } from "zod";

export default function Form(){
const router = useRouter();
const handleSubmit = async (e: FormEvent<HTMLFormElement>) => {
e.preventDefault();

const formData = new FormData(e.currentTarget);
const response = await signIn('credentials', {
email: formData.get("email"),
password: formData.get("password"),
redirect: false,
});
console.log({response});
if (!response?.error) {
router.push('/');
router.refresh();
}
};

export function Form(){
return (
<div className="flex items-center flex-col w-auto gap-8 font-sans">
<h1 className='text-2xl font-sans font-normal '>
Login
</h1>
<div className=" bg-slate-900 w-full h-0.5 rounded-sm"> </div>
<form onSubmit={handleSubmit} className="flex flex-col gap-4">
<form>
<Label htmlFor="email">Email</Label>

<Input name="email" type="email" placeholder="Email"></Input>
<Label htmlFor="password">Password</Label>

<Input name="password" type="password" placeholder="Password"></Input>
<div className=" bg-slate-900 h-0.5 rounded-sm"> </div>

<Button className="my-5" type="submit">Login</Button>
Expand Down
4 changes: 2 additions & 2 deletions saraia/app/loginform/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Form from "./form";
import {Form} from "./form";
import {Input} from "@/components/ui/input";

export default function LoginPage(){
export default function LogisnPage(){
return (
<Form />
)
Expand Down
64 changes: 22 additions & 42 deletions saraia/auth/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,52 +6,32 @@ import { User } from "next-auth";
import { DrizzleAdapter } from "@auth/drizzle-adapter";
import NextAuth from "next-auth";
import * as tables from "@/lib/schema"
import { table } from "console";
import Resend from "next-auth/providers/resend"
import {loadEnvConfig} from "@next/env";
import { config } from "process";
const projectDir = process.cwd();
loadEnvConfig(projectDir);


declare module "next-auth" {
interface AdapterUser extends User {userID:number}

}


export const { auth, handlers, signIn, signOut } = NextAuth({
adapter: DrizzleAdapter(db, {
usersTable: tables.users,
accountsTable: tables.account,
sessionsTable: tables.sessions,
}),
providers: [CredentialsProvider({
credentials: {
email: { type: 'text' },
password: { type: 'password' },
},
async authorize(credentials, req) {
try {
if (!credentials || !credentials.email || !credentials.password) {
throw new Error("Missing credentials");
}

const response = await db.execute(sql`SELECT * FROM users WHERE email=${credentials.email}`);
const user = response.rows[0];
if (!user) {
throw new Error("No user found with this email");
}

const password = user.password as string;
const passwordCorrect = await compare(credentials.password, password);

if (passwordCorrect) {
return {
email: user.email as string,
name: user.username as string,
userID: user.id_user as number,
} as User;
} else {
throw new Error("Incorrect password");
adapter: DrizzleAdapter(db),
providers:[Resend({
apiKey: process.env.AUTH_RESEND_KEY,
id:'email',
name:'email',
server: {
host: process.env.EMAIL_SERVER_HOST,
port: process.env.EMAIL_SERVER_PORT,
auth: {
user: process.env.EMAIL_SERVER_USER,
pass: process.env.EMAIL_SERVER_PASSWORD
}
} catch (error) {
console.error("Authorization error:", error);
return null; // Return null in case of any errors during authorization
}
},
}),],
},
from:process.env.EMAIL_FROM,
}
)],
})
112 changes: 112 additions & 0 deletions saraia/drizzle/0000_skinny_shape.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
-- Current sql file was generated after introspecting the database
-- If you want to run this migration please uncomment this code before executing migrations
/*
CREATE TABLE IF NOT EXISTS "admins" (
"id_admin" serial PRIMARY KEY NOT NULL,
"id_user" integer
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "jobtitles" (
"id_jobtitle" serial PRIMARY KEY NOT NULL,
"title" varchar(50) NOT NULL
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "users" (
"id_user" serial PRIMARY KEY NOT NULL,
"username" varchar(50) NOT NULL,
"password" varchar(73) NOT NULL,
"datecreated" timestamp DEFAULT now(),
"creatoradminid" integer,
"email" varchar(320),
"emailverified" timestamp,
"image" text
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "team" (
"id_team" serial PRIMARY KEY NOT NULL,
"team_name" varchar(50) NOT NULL
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "userteamposition" (
"id_ustepo" serial PRIMARY KEY NOT NULL,
"id_user" integer,
"id_team" integer,
"id_jobtitle" integer
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "feedback" (
"id_feedback" serial PRIMARY KEY NOT NULL,
"Performance" integer,
"well_being" integer,
"flow" integer,
"communication" integer,
"activity" integer,
"collaboration" integer,
"efficiency" integer,
"satisfaction" integer,
"thread_id" varchar(64),
"id_user" integer,
"id_team" integer,
"feedback_time" timestamp
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "messages" (
"id_message" serial PRIMARY KEY NOT NULL,
"message" varchar(9999),
"messagedate" timestamp with time zone DEFAULT now(),
"id_user" integer,
"id_team" integer,
"email" varchar(320),
"thread_id" varchar(64)
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "user_messages" (
"id_usermessage" serial PRIMARY KEY NOT NULL,
"message" varchar(9999),
"messagedate" timestamp with time zone DEFAULT now(),
"id_user" integer,
"id_team" integer,
"email" varchar(320),
"thread_id" varchar(64)
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "session" (
"sessionToken" text PRIMARY KEY NOT NULL,
"userid" integer,
"expires" timestamp
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "account" (
"compoundkey" integer PRIMARY KEY NOT NULL,
"userid" integer,
"type" text,
"provider" text,
"provideraccountid" text,
"refresh_token" text,
"access_tokken" text,
"expires_at" integer,
"token_type" text,
"scope" text,
"id_tokken" text,
"session_state" text
);
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "userteamposition" ADD CONSTRAINT "userteamposition_id_jobtitle_jobtitles_id_jobtitle_fk" FOREIGN KEY ("id_jobtitle") REFERENCES "public"."jobtitles"("id_jobtitle") ON DELETE no action ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "session" ADD CONSTRAINT "fk_id_puserid" FOREIGN KEY ("userid") REFERENCES "public"."users"("id_user") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "account" ADD CONSTRAINT "fk_id_auserid" FOREIGN KEY ("userid") REFERENCES "public"."users"("id_user") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
*/
Loading

0 comments on commit 665781c

Please sign in to comment.