Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Frontend PR #3

Open
wants to merge 9 commits into
base: main2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions HOC/withAuth.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { useRouter } from "next/router";
import { useUsersContext } from '../context/UsersContext';
import { useMemo,useEffect, useCallback, useState } from 'react';
//import { useState } from "react/cjs/react.development";
import { setAuth } from "../utils/setAuth";
import axios from "axios";
import { SERVER_URL } from "../server";

const withAuth = (WrappedComponent) => {
// eslint-disable-next-line react/display-name
return (props) => {

const {user,setUser}=useUsersContext()

const loadUser=useCallback( async (localToken)=>{
setAuth(localToken)
try {
const res = await axios.get(`${SERVER_URL}/api/users/me`);
const{email,name,avatar}=res.data
setUser({email,name,avatar})
}
catch (error) {
console.log(error)
}
},[setUser])

useEffect(() => {
const localToken=localStorage.getItem("token")
loadUser(localToken)
}, [loadUser])

if (typeof window !== "undefined") {

const Router = useRouter();
const accessToken = localStorage.getItem("token");

if (!accessToken) {
Router.replace("/");
return null;
}

return <WrappedComponent {...props} />;
}
return null;
};
};

export default withAuth;
Binary file added assets/bg.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/bg.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/chat.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions assets/logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions context/UsersContext.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { createContext, useContext } from 'react';
import { useState } from 'react/cjs/react.development';

const UsersContext = createContext();

export function UsersWrapper({ children }) {
const [user,setUser]= useState([])
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is user an array?

return (
<UsersContext.Provider value={{user,setUser}}>
{children}
</UsersContext.Provider>
);
}

export function useUsersContext() {
return useContext(UsersContext);
}
14 changes: 12 additions & 2 deletions next.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
// module.exports = {
// reactStrictMode: true,
// }
module.exports = {
reactStrictMode: true,
}
async rewrites() {
return [
{
source: '/api/:path*',
destination: 'https://betatech-chat.herokuapp.com/:path*',
},
]
},
};
Loading