Skip to content

Commit

Permalink
feat(auth): keep user auth if his data is stored in localStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
danilych committed Feb 10, 2024
1 parent c713ebd commit d036661
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
27 changes: 22 additions & 5 deletions app/root.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { cssBundleHref } from '@remix-run/css-bundle'
import { json} from '@remix-run/node';
import type { LoaderFunction , LinksFunction } from '@remix-run/node';
import { json } from '@remix-run/node'
import type { LoaderFunction, LinksFunction } from '@remix-run/node'
import {
Links,
LiveReload,
Expand All @@ -14,8 +14,10 @@ import {
import stylesheet from '~/tailwind.css'
import Navigation from './widgets/navigation'
import Footer from './widgets/footer'
import store from './redux/store';
import { Provider } from 'react-redux';
import store from './redux/store'
import { Provider, useDispatch } from 'react-redux'
import type { ThunkDispatch } from '@reduxjs/toolkit'
import { fetchAuth } from './redux/slices/auth'

export const loader: LoaderFunction = async () => {
return json({
Expand All @@ -34,6 +36,21 @@ export let handle = {
i18n: 'common',
}

export function OutletProvider() {
const dispatch = useDispatch<ThunkDispatch<any, any, any>>()

if (typeof window !== 'undefined') {
let requestData = {
Email: window.localStorage.getItem('email'),
Password: window.localStorage.getItem('password'),
}

dispatch(fetchAuth(requestData))
}

return <Outlet />
}

export default function App() {
const data = useLoaderData<typeof loader>()
return (
Expand All @@ -47,7 +64,7 @@ export default function App() {
<body>
<Provider store={store}>
<Navigation />
<Outlet />
<OutletProvider />
<ScrollRestoration />
<Scripts />
<LiveReload />
Expand Down
3 changes: 1 addition & 2 deletions app/routes/my-account._index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { ThunkDispatch } from '@reduxjs/toolkit'
import type { ThunkDispatch } from '@reduxjs/toolkit'
import { type V2_MetaFunction } from '@remix-run/node'
import { EmptyAvatar } from 'assets/images'
import { Spinner, Textarea } from 'flowbite-react'
import moment from 'moment'
import { useEffect, useState } from 'react'
import { useDispatch, useSelector } from 'react-redux'
import { selectIsAuth } from '~/redux/slices/auth'
Expand Down
2 changes: 2 additions & 0 deletions app/widgets/login-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ export default function LoginCard() {
console.log(data.payload)

window.localStorage.setItem('userId', data.payload.userID)
window.localStorage.setItem('email', values.Email)
window.localStorage.setItem('password', values.Password)
} catch (error) {
toast.error('Something went wrong!')
}
Expand Down
4 changes: 2 additions & 2 deletions app/widgets/register-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ export default function RegisterCard() {
}

window.localStorage.setItem('userId', data.payload.id)
window.localStorage.setItem('email', data.payload.email)
window.localStorage.setItem('name', data.payload.userName)
window.localStorage.setItem('email', values.Email)
window.localStorage.setItem('password', values.Password)
} catch (error) {
toast.error('Something went wrong!')
}
Expand Down

0 comments on commit d036661

Please sign in to comment.