https://github.com/dipeshrai123/next-auth-navigation
NextJS Library for authentication
https://github.com/dipeshrai123/next-auth-navigation
authentication next-auth-navigation nextjs
Last synced: 6 months ago
JSON representation
NextJS Library for authentication
- Host: GitHub
- URL: https://github.com/dipeshrai123/next-auth-navigation
- Owner: dipeshrai123
- License: mit
- Created: 2020-12-24T07:11:42.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2020-12-27T05:23:54.000Z (about 5 years ago)
- Last Synced: 2023-03-04T02:32:39.251Z (almost 3 years ago)
- Topics: authentication, next-auth-navigation, nextjs
- Language: TypeScript
- Homepage:
- Size: 286 KB
- Stars: 6
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Next Auth Navigation
> NextJS Library for authentication
[](https://www.npmjs.com/package/react-auth-navigation) [](https://standardjs.com)
## Install
```bash
// with npm
npm i next-auth-navigation
// with yarn
yarn add next-auth-navigation
```
## Why is next-auth-navigation ?
It is a NextJS Library for user authentication in client / server side. It provides basic HOC's that to wrap around pages to authenticate user very easily.
## Usage
### Authentication
**next-auth-navigation** only provides 2 helpful hocs to define user authentication in client / server side. Both hocs must be used for authentication.
- **withAuth()** and
- **withAuthServerSideProps()**
#### withAuth(_Component_, _options?_)
**withAuth()** accepts a _component_ for which we wan an authentication as a first argument and _options_ as a second argument. _options_
Let us configure the _second argument_.
- **redirectUri** _( optional )_ : If the user is not logged in, user is redirected to path assigned in _redirectUri_.
- **authenticatedUri** _( optional )_ : If the user is logged in, user is redirected to path assigned in _authenticatedUri_.
- **FallbackComponent** _( optional )_ : If the user is not logged in, Fallback component is shown on a page.
- **FeedbackComponent** _( optional )_ : Feedback component is shown when redirecting either on _redirectUri_ or _authenticatedUri_.
#### withAuthServerSideProps(_options?_, _callback?_)
**withAuthServerSideProps()** is used instead of **getServerSideProps()** function on a page with **withAuth()** hoc. It is used to authenticate user in server-side. **options** is passed as a first optional argument where we can specify _redirectUri_ or _authenticatedUri_ for server-side redirection. **callback** function can be provided as second argument which acts as a **getServerSideProps()** function with _context_ as a first argument and _data_ as a second argument reffering all _cookie data_.
**Example**
```javascript
import { withAuth, withAuthServerSideProps } from "next-auth-navigation";
function Home() {
return
HOME PAGE;
}
export default withAuth(Home, {
redirectUri: "/login",
});
export const getServerSideProps = withAuthServerSideProps();
```
## License
MIT © [dipeshrai123](https://github.com/dipeshrai123)