Last active
June 5, 2022 13:59
-
-
Save sourabhbagrecha/2649156029a7f9d5c9a454e740e49c83 to your computer and use it in GitHub Desktop.
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 { createContext, useState } from "react"; | |
import { App, Credentials } from "realm-web"; | |
import { APP_ID } from "../realm/constants"; | |
// Creating a Realm App Instance | |
const app = new App(APP_ID); | |
// Creating a user context to manage and access all the user related functions | |
// across different component and pages. | |
export const UserContext = createContext(); | |
export const UserProvider = ({ children }) => { | |
const [user, setUser] = useState(null); | |
// Function to login user into our Realm using their email & password | |
const emailPasswordLogin = async (email, password) => { | |
const credentials = Credentials.emailPassword(email, password); | |
const authedUser = await app.logIn(credentials); | |
setUser(authedUser); | |
return authedUser; | |
}; | |
// Function to signup user into our Realm using their email & password | |
const emailPasswordSignup = async (email, password) => { | |
try { | |
await app.emailPasswordAuth.registerUser(email, password); | |
// Since we are automatically confirming our users we are going to login | |
// the user using the same credentials once the signup is complete. | |
return emailPasswordLogin(email, password); | |
} catch (error) { | |
throw error; | |
} | |
}; | |
// Function to fetch-user(if the user is already logged in) from local storage | |
const fetchUser = async () => { | |
if (!app.currentUser) return false; | |
try { | |
await app.currentUser.refreshCustomData(); | |
// Now if we have a user we are setting it to our user context | |
// so that we can use it in our app across different components. | |
setUser(app.currentUser); | |
return app.currentUser; | |
} catch (error) { | |
throw error; | |
} | |
} | |
// Function to logout user from our Realm | |
const logOutUser = async () => { | |
if (!app.currentUser) return false; | |
try { | |
await app.currentUser.logOut(); | |
// Setting the user to null once loggedOut. | |
setUser(null); | |
return true; | |
} catch (error) { | |
throw error | |
} | |
} | |
return <UserContext.Provider value={{ user, setUser, fetchUser, emailPasswordLogin, emailPasswordSignup, logOutUser }}> | |
{children} | |
</UserContext.Provider>; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment