Skip to main content

MoopsyClientAuthExtension

The Moopsy Client Auth Extension handles client-side authentication for your Moopsy app.

class
MoopsyClientAuthExtension
<AuthSpec>
(client, params)
Class
import { MoopsyClientAuthExtension } from "@moopsyjs/react";
<>
Type Arguments
AuthSpec:
MoopsyAuthenticationSpec
()
Arguments
params:
{
autoLoginFunction: 
() => AuthRequestType of AuthSpec (Function)
}
Methods / Properties
logout()=> void
login(params: AuthRequestType of AuthSpec)=> void
useAuthStatus()=> React Hook<MoopsyClientAuthExtensionState>
useAuthState()=> React Hook<PublicAuthType of AuthSpec | null>

autoLoginFunction gets called every time a new Moopsy connection is established. This happens for the initial connection after MoopsyClient is instantiated, and every time a new connection is created (for example, if the internet cuts out and Moopsy cannot recover the existing connection, or if the server it is connected to shuts down).

moopsyAuth.
MoopsyClientAuthExtension
useAuthStatus
()
âš› React Hook (Function)
()
Arguments
None
=>
Returns
Value of
MoopsyClientAuthExtensionState (Enum)
"loggedIn" | "loggingIn" | "loggedOut"

Usage​

// This should be defined in common/ and shared across client & server
type AuthSpec = {
PublicAuthType: {
userId: string
},
AuthRequestType: {
token: string
}
}

export const moopsyAuth = new MoopsyClientAuthExtension<AuthSpec>(moopsyClient, {
autoLoginFunction: async () => {
const token = window.sessionStorage.token;

if (token != null) {
return { token };
}

return null;
}
});

const onLogin = () => {
void someMutationThatReturnsAToken().then(({token}) => {
moopsyAuth.login({token}).then(() => {
// Login was a success, save the token
window.sessionStorage.setItem("token", token);
});
});
}