Skip to content

Commit

Permalink
Make login page interact with backend (#16)
Browse files Browse the repository at this point in the history
* Added appwrite package

* Access appwrite config from env

* Added logging service

* Added appwrite in _app

* Added network later

* Added post request in _app

* Added react toast

* Added login api

* Added toast and oauth 2 handler

* Updated _app.js
  • Loading branch information
Harsh3305 authored Jun 29, 2023
1 parent 3413f90 commit 748d620
Show file tree
Hide file tree
Showing 7 changed files with 329 additions and 7 deletions.
141 changes: 141 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
"lint": "next lint"
},
"dependencies": {
"appwrite": "^11.0.0",
"eslint": "8.42.0",
"eslint-config-next": "13.4.5",
"next": "13.4.5",
"react": "18.2.0",
"react-dom": "18.2.0",
"react-icons": "^4.10.1",
"react-toastify": "^9.1.3",
"typewriter-effect": "^2.20.1"
}
}
65 changes: 61 additions & 4 deletions src/pages/_app.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,68 @@
import '@/styles/globals.css'
import Header from "@/component/header";
import Footer from "@/component/footer";
import {useEffect, useState} from "react";
import {logError, logMessage} from "@/service/logging/logging";
import {Account, Client} from "appwrite";
import {postRequest} from "@/service/network/network";
import 'react-toastify/dist/ReactToastify.css';
import {ToastContainer} from "react-toastify";

export default function App({ Component, pageProps }) {
const appwrite_endpoint = process.env.NEXT_PUBLIC_APPWRITE_ENDPOINT;
const appwrite_project = process.env.NEXT_PUBLIC_APPWRITE_PROJECT_ID;
const application_url = process.env.NEXT_PUBLIC_APPLICATION_URL;

const [details, setDetails] = useState({
account: null,
token: null
})
logMessage(details)

useEffect(()=>{
generateToken().then().catch(logError);
setInterval(generateToken, 1000*60*10);
}, [])

return <>
<Header/>
<Component {...pageProps} />
<Footer/>
<Header token={details.token} />
<Component {...pageProps} token={details.token} account={details.account} application_url={application_url} />
<Footer />
<ToastContainer />
</>
}

async function generateToken() {

const client = new Client()
client.setEndpoint(appwrite_endpoint)
client.setProject(appwrite_project)

const account = new Account(client)
var token = null

try {
const response = await account.createJWT();
const jwt = response.jwt;

const result = await postRequest(
'/api/login',
{
jwt: jwt
},
{
"Content-Type": "application/json"
},
false
)

token = result.data
}
catch (error) {
logError(error)
}
setDetails({
account: account,
token: token
})
}
}
23 changes: 23 additions & 0 deletions src/pages/api/login.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {postRequest} from "@/service/network/network";

export default async function handler(req, res) {
if (req.method === "POST") {
try {
const response = await postRequest(
`${process.env.BACKEND_URL}/auth/login`,
req.body.jwt,
{
"Content-Type": "text/plain"
},
false
);
res.status(response.status).json(response.data);
}
catch (_) {
res.status(404).json({message: "Auth not found"})
}
}
else {
res.status(404).json({message: "Not found"})
}
}
Loading

0 comments on commit 748d620

Please sign in to comment.