Hello! I’m developing a site with Front (React) & Back (NestJS - RWA). I made login / logout like in docs, but i have an a question how to login in Front using our BackEnd?
BackEnd:
main.ts
...
const configAuth0 = {
authRequired: false,
auth0Logout: true,
secret: 'a long, randomly-generated string stored in env',
baseURL: 'http://localhost:5000',
clientID: '_',
issuerBaseURL: '_'
};
app.use(auth(configAuth0));
...
app.controller.ts
@Controller()
export class AppController {
constructor() {
}
@Get()
@Header('Access-Control-Allow-Origin', '*')
async isAuthenticated(@Req() req: Request, @Res() res: Response){
res.send(req.oidc.isAuthenticated ? "Logged in" : "Logged out");
}
}
And Front:
function App() {
const [isAuth, setIsAuth] = useState(false);
function isUserAuth() {
axios.get('http://localhost:5000')
.then(value => setIsAuth(value.data))
.catch(reason => console.log(reason));
}
useEffect(() => {
isUserAuth();
})
return (
<div className="App">
<header className="App-header">
<button onClick={() => window.location.href = 'http://localhost:5000/login'}>Log In</button>
<button onClick={() => window.location.href = 'http://localhost:5000/logout'}>Log Out</button>
<div>
<h2>{isAuth.valueOf()}</h2>
</div>
<p>
Edit <code>src/App.js</code> and save to reload.
</p>
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
Learn React
</a>
</header>
</div>
);
}
export default App;
Front - localhost:3000. Back - localhost:5000
Application URIs:
Thanks for your help!