The useLogin hook is imported from the '@magicjs.dev/frontend' and is used to get the current object, which contains the information about the current user's details.
An <h1> element is used to display the name of the current user.
The ?. operator is used for optional chaining, ensuring that the code does not throw an error if current or currentUser is undefined or null.
Implementing Login Functionality
Let's develop the login functionality by creating a backend file named "login".
Create a backend file named "login".
The 'server.tsx' extension will be automatically appended in MERN.AI.
Write the login logic within the backend file, including the input "name: string" obtained from the frontend.
Call the ctx.setCurrentUser() function to set the current user.
Refer the snippet below.
login.server.tsx
import { createBackendFunction, useFunctionContext } from"@magicjs.dev/backend"exportdefaultcreateBackendFunction(asyncfunction (name:string) {constctx=useFunctionContext(this)ctx.setCurrentUser({ _id:"test-user", name })return"Message from server"})
The function takes a single parameter name, which is expected to be a string. This parameter represents the name of the user being set as the current user.
Inside the function body, the useFunctionContext function is called with this as its argument.
The ctx.setCurrentUser method is called with an object containing _id and name properties. This method is provided by the @magicjs.dev/backend library for setting the current user within the backend function's context.
The function returns the string "Message from server". This is the response that will be sent back to the client when the backend function is invoked.
Triggering the Function with the onClick Prop
Refer the below code to call this function in the frontend file "home.tsx".
The logout functionality is seamlessly integrated into the application without the need for a separate backend function. This capability is provided by the useLogin hook imported from '@magicjs.dev/frontend', simplifying the implementation of authentication features.
Implementing Conditional Rendering
Let's make the Logout button invisible when a user is not logged in, by wrapping it within a conditional statement.
The "Logout" button is conditionally rendered using (current.isAuthenticated === true ? ... : null). This ensures that the "Logout" button is only displayed when the user is authenticated or logged in.
Implementing Backend Authentication
In addition to the frontend authentication, we'll also explore backend authentication to further enhance the security of the application. This includes securing backend APIs and endpoints to ensure that only authenticated users can access protected resources.
Create a backend file named "get-my-name" in the main-features directory.
Create a conditional statement to check if the user is authenticated. If authenticated, return the current user's name; otherwise, return "User not signed in".
Refer the snippet below
get-my-name.server.tsx
import { createBackendFunction, useFunctionContext } from"@magicjs.dev/backend"exportdefaultcreateBackendFunction(asyncfunction () {constctx=useFunctionContext(this)if (ctx.isAuthenticated ===false) {return"User not signed in" }returnctx.currentUser.name;})
Triggering the Protected Backend
In the 'home.tsx' file, import getMyNameServer in the frontend and call the function using the button.
The above update in the code renders a <Button> component labelled "Call Protected Backend".
When this button is clicked, it triggers an asynchronous operation to call a function named getMyNameServer().
Upon successfully resolving the promise returned by getMyNameServer(), the function alert() is invoked to display the response from getMyNameServer().
A pre-developed magic feature for authentication is available in mern.ai. You can simply import the feature and integrate it to your project. Visit mern.ai for more information.
Congratulations! You've learned how to authenticate a user seamlessly in MERN.AI using the MagicJS framework.