Last active
May 17, 2020 01:48
-
-
Save ryanbelke/52e1651590fe2218d931ff0b66c3308f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* /lib/auth.js */ | |
import { useEffect } from "react"; | |
import Router from "next/router"; | |
import Cookie from "js-cookie"; | |
import axios from "axios"; | |
const API_URL = process.env.NEXT_PUBLIC_API_URL || "http://localhost:1337"; | |
//register a new user | |
export const registerUser = (username, email, password) => { | |
//prevent function from being ran on the server | |
if (typeof window === "undefined") { | |
return; | |
} | |
return new Promise((resolve, reject) => { | |
axios | |
.post(`${API_URL}/auth/local/register`, { username, email, password }) | |
.then((res) => { | |
//set token response from Strapi for server validation | |
Cookie.set("token", res.data.jwt); | |
//resolve the promise to set loading to false in SignUp form | |
resolve(res); | |
//redirect back to home page for restaurance selection | |
Router.push("/"); | |
}) | |
.catch((error) => { | |
//reject the promise and pass the error object back to the form | |
reject(error); | |
}); | |
}); | |
}; | |
export const login = (identifier, password) => { | |
//prevent function from being ran on the server | |
if (typeof window === "undefined") { | |
return; | |
} | |
return new Promise((resolve, reject) => { | |
axios | |
.post(`${API_URL}/auth/local/`, { identifier, password }) | |
.then((res) => { | |
//set token response from Strapi for server validation | |
Cookie.set("token", res.data.jwt); | |
//resolve the promise to set loading to false in SignUp form | |
resolve(res); | |
//redirect back to home page for restaurance selection | |
Router.push("/"); | |
}) | |
.catch((error) => { | |
//reject the promise and pass the error object back to the form | |
reject(error); | |
}); | |
}); | |
}; | |
export const logout = () => { | |
//remove token and user cookie | |
Cookie.remove("token"); | |
delete window.__user; | |
// sync logout between multiple windows | |
window.localStorage.setItem("logout", Date.now()); | |
//redirect to the home page | |
Router.push("/"); | |
}; | |
//Higher Order Component to wrap our pages and logout simultaneously logged in tabs | |
// THIS IS NOT USED in the tutorial, only provided if you wanted to implement | |
export const withAuthSync = (Component) => { | |
const Wrapper = (props) => { | |
const syncLogout = (event) => { | |
if (event.key === "logout") { | |
Router.push("/login"); | |
} | |
}; | |
useEffect(() => { | |
window.addEventListener("storage", syncLogout); | |
return () => { | |
window.removeEventListener("storage", syncLogout); | |
window.localStorage.removeItem("logout"); | |
}; | |
}, []); | |
return <Component {...props} />; | |
}; | |
if (Component.getInitialProps) { | |
Wrapper.getInitialProps = Component.getInitialProps; | |
} | |
return Wrapper; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment