Skip to content

Commit

Permalink
Implement Sign Up Component with Next.js (#67)
Browse files Browse the repository at this point in the history
* adding a login page but it need a auth

* adding sign up componenet
  • Loading branch information
yassineboujrada committed Jul 21, 2023
1 parent e2227f9 commit 0140aa1
Show file tree
Hide file tree
Showing 8 changed files with 190 additions and 6 deletions.
File renamed without changes
File renamed without changes
3 changes: 3 additions & 0 deletions frontend-app/.env.local
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
DATA_API_KEY = workflow team
DATA_SOURCE_URL = http://localhost:5000/api/v1/auth/
NODE_ENV = production
2 changes: 1 addition & 1 deletion frontend-app/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ yarn-debug.log*
yarn-error.log*

# local env files
.env*.local
# .env*.local

# vercel
.vercel
Expand Down
37 changes: 33 additions & 4 deletions frontend-app/src/app/login/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,36 @@ import React, { useState } from "react";

export default function page() {

const DATA_SOURCE_URL: string = process.env.DATA_SOURCE_URL as string
const API_KEY: string = process.env.DATA_API_KEY as string

const [email, setEmail] = useState('');
const [password, setPassword] = useState('');

const submitHandler = (e: React.FormEvent) => {
const submitHandler = async (e: React.FormEvent) => {
e.preventDefault();
// Add your login logic here
console.log(email, password)

if ( !email || !password ) console.log("Missing required data");

try {
const res = await fetch(DATA_SOURCE_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'API-Key': API_KEY
},
body: JSON.stringify({
email,password
})
})
const data = await res.json()
console.log(data)
} catch (error) {
console.log(error)
}
};


return (
<section className="border-red-500 bg-gray-200 min-h-screen flex items-center justify-center">
<div className="bg-gray-100 p-5 flex rounded-2xl shadow-lg max-w-3xl">
Expand Down Expand Up @@ -75,9 +96,17 @@ export default function page() {
<span className="ml-4 text-black">Login with Google</span>
</button>

<button className="bg-white border py-2 w-full rounded-xl mt-5 flex justify-center items-center text-sm hover:scale-105 duration-300 "
onClick={() => {window.location.href="https://github.com/login/oauth/authorize?client_id=Iv1.6d6db037d6c883d9&redirect_url=http://localhost:4000/api/v1/auth/github/callback?path=/&scope=user:yassine.boujrada@gmail.com"}}>
<svg xmlns="http://www.w3.org/2000/svg" fill="currentColor" className="w-6 h-6" viewBox="0 0 16 16">
<path d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82.64-.18 1.32-.27 2-.27.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.012 8.012 0 0 0 16 8c0-4.42-3.58-8-8-8z"/>
</svg>
<span className="ml-4 text-black">Login with Github</span>
</button>

<div className="text-sm flex justify-between items-center mt-3 text-black">
<p>If you dont have an account </p>
<button className="py-2 px-5 ml-3 bg-white border rounded-xl hover:scale-110 duration-300 border-blue-400 text-black">Register</button>
<Link href="/signup" className="py-2 px-5 ml-3 bg-white border rounded-xl hover:scale-110 duration-300 border-blue-400 text-black">Sign Up</Link>
</div>
</div>

Expand Down
116 changes: 116 additions & 0 deletions frontend-app/src/app/signup/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/* eslint-disable react-hooks/rules-of-hooks */
"use client";

import Link from "next/link";
import React, { useState } from "react";

export default function page() {

const DATA_SOURCE_URL: string = process.env.DATA_SOURCE_URL as string
const API_KEY: string = process.env.DATA_API_KEY as string

const [name, setName] = useState('');
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [passwordConfirm,setPasswordConfirm] = useState('');

const submitHandler = async (e: React.FormEvent) => {
e.preventDefault();

if ( !name || !email || !password || !passwordConfirm ) console.log("Missing required data");

try {
const res = await fetch(DATA_SOURCE_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'API-Key': API_KEY
},
body: JSON.stringify({
name,email,password,passwordConfirm
})
})
const data = await res.json()
console.log(data)
} catch (error) {
console.log(error)
}

};

return (
<section className="border-red-500 bg-gray-200 min-h-screen flex items-center justify-center">
<div className="bg-gray-100 p-5 flex rounded-2xl shadow-lg max-w-3xl w-8/12 h-1/2">
<div className="md:w-1/2 px-5">
<h2 className="text-2xl font-bold text-[#002D74]">Sign Up</h2>
<p className="text-sm mt-4 text-[#002D74]">Welcome to our plateform</p>
<form className="mt-6" onSubmit={submitHandler}>

<div>
<label className="block text-gray-700">Username</label>
<input
type="text"
placeholder="Enter Username"
className="w-full px-4 py-3 rounded-lg bg-gray-200 mt-2 border focus:border-blue-500 focus:bg-white focus:outline-none text-black"
value={name}
onChange={(e) => setName(e.target.value)}
autoFocus required/>
</div>

<div className="mt-4">
<label className="block text-gray-700">Email Address</label>
<input
type="email"
placeholder="Enter Email Address"
className="w-full px-4 py-3 rounded-lg bg-gray-200 mt-2 border focus:border-blue-500 focus:bg-white focus:outline-none text-black"
value={email}
onChange={(e) => setEmail(e.target.value)}
autoFocus required/>
</div>

<div className="mt-4">
<label className="block text-gray-700">Password</label>
<input
type="password"
placeholder="Enter Password"
className="w-full px-4 py-3 rounded-lg bg-gray-200 mt-2 border focus:border-blue-500 focus:bg-white focus:outline-none text-black"
value={password}
onChange={(e) => setPassword(e.target.value)}
required/>
</div>

<div className="mt-4">
<label className="block text-gray-700">Confirme Your Password</label>
<input
type="password"
placeholder="Confirm Password"
className="w-full px-4 py-3 rounded-lg bg-gray-200 mt-2 border focus:border-blue-500 focus:bg-white focus:outline-none text-black"
value={passwordConfirm}
onChange={(e) => setPasswordConfirm(e.target.value)}
required/>
</div>

<button type="submit" className="w-full block bg-blue-500 hover:bg-blue-400 focus:bg-blue-400 text-white font-semibold rounded-lg
px-4 py-3 mt-6">Sign Up
</button>
</form>

<div className="mt-7 grid grid-cols-3 items-center text-gray-500">
<hr className="border-gray-500" />
<hr className="border-gray-500" />
</div>

<div className="text-sm flex justify-between items-center mt-3 text-black">
<p>If you have an account </p>
<Link href="/login" className="py-2 px-5 ml-3 bg-white border rounded-xl hover:scale-110 duration-300 border-blue-400 text-black">Login</Link>
</div>
</div>

<div className="w-1/2 h-1/4 md:block hidden">
<img src="/logo.jpg" className="rounded-2xl" alt="page img" />
</div>

</div>
</section>
);
};
34 changes: 34 additions & 0 deletions frontend-app/src/middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { NextResponse } from "next/server"

const allowedOrigins = process.env.NODE_ENV === 'production'
? ['http://localhost:5000', 'http://localhost:4000']
: ['http://localhost:3000']

export function middleware(request: Request) {

const origin = request.headers.get('origin')
console.log(origin)

if (origin && !allowedOrigins.includes(origin)) {
return new NextResponse(null, {
status: 400,
statusText: "Bad Request",
headers: {
'Content-Type': 'text/plain'
}
})
}

console.log('Middleware!')

console.log(request.method)
console.log(request.url)



return NextResponse.next()
}

export const config = {
matcher: '/api/:path*',
}
4 changes: 3 additions & 1 deletion frontend-app/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
type UserData = {
name?: string,
email?: string,
password?: string
password?: string,
confirmPassword?: string,
}

0 comments on commit 0140aa1

Please sign in to comment.