Last active
February 1, 2023 22:46
-
-
Save Neeraj1005/b543627c75d531fdaccef17b2200fab6 to your computer and use it in GitHub Desktop.
it is work as route middleware if user is not logged then go back to login page.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Cookies from 'js-cookie'; | |
import Route from 'next/router'; | |
export const TOKEN_STORAGE_KEY = 'token'; | |
export const USER_STORAGE_KEY = 'userName'; | |
class AuthService { | |
static getUserAuthHeader = () => { | |
const token = Cookies.get(TOKEN_STORAGE_KEY); | |
return { | |
Authorization: 'Bearer ' + token, | |
}; | |
}; | |
static handleLogin = async (email, password) => { | |
const res = await fetch(`${YOUR_LOGIN_URL`, { | |
method: "POST", | |
body: JSON.stringify({ | |
email, | |
password, | |
}), | |
headers: { | |
"Content-Type": "application/json", | |
}, | |
}) | |
const result = await res.json() | |
if (res.status !== 200) alert('Please try to login again.'); | |
if (res.status === 200) { | |
Cookies.set(TOKEN_STORAGE_KEY, result.access_token); | |
Cookies.set(USER_STORAGE_KEY, result.clientData.name); | |
Route.push('/dashboard'); | |
} | |
}; | |
} | |
export default AuthService; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { ProtectedRoute } from '@/components/High-order-component' | |
import React from 'react' | |
const Dashboard = () => { | |
return ( | |
<div> | |
<h3> | |
welcome to dashboard | |
</h3> | |
</div> | |
) | |
} | |
export default ProtectedRoute(Dashboard) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component } from "react"; | |
import cookies from "next-cookies"; | |
import { TOKEN_STORAGE_KEY, USER_STORAGE_KEY } from "@/services/auth.service"; | |
export function ProtectedRoute(WrapperComponent) { | |
return class extends Component { | |
static async getInitialProps(ctx) { | |
const token = cookies(ctx)[TOKEN_STORAGE_KEY]; | |
if (!token) { | |
// const route = '/login?dest=' + ctx.asPath; | |
const route = "/login"; | |
if (ctx.res) { | |
ctx.res.writeHead(302, { Location: route }); | |
ctx.res.end(); | |
} | |
} | |
const initialProps = { | |
token, | |
userName: cookies(ctx)[USER_STORAGE_KEY], | |
query: ctx.query, | |
asPath: ctx.asPath, | |
}; | |
if (WrapperComponent.getInitialProps) | |
return WrapperComponent.getInitialProps(initialProps); | |
return initialProps; | |
} | |
render() { | |
const { ...propsWithoutAuth } = this.props; | |
return <WrapperComponent {...propsWithoutAuth} />; | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment