Skip to main content

Authentication - SPA

Example Authentication - SPA


This example focuses on performing a game client authentication using a Single Page Application architecture. The example utilizes React as the SPA library of choice. The example will show a page with a login button when not authenticated to the Haste Arcade ecosystem, but if you are authenticated it will show your profile details.

Link to Github

Run Example

In order to run the example please perform the following commands:

git clone
cd haste-sdk/examples/authentication/single-page-app/
npm install
npm start


import React, { useEffect, useState } from "react";
import badge from "./login.svg";
import "./App.css";
import { HasteClient } from "@hastearcade/web";

function App() {
// If you are using React, consider using Context
// for storing the HasteClient rather than State
// This was used for simplicity.

// For the Haste Client specifically, please look at implementing
// a provider.
const [hasteClient, setHasteClient] = useState();
const [picture, setPicture] = useState("");
const [displayName, setDisplayName] = useState("");
const [isAuthenticated, setAuthenticated] = useState(false);

useEffect(() => {
const hasteClient =;
}, []);

useEffect(() => {
// retrieving the token details requires
// an async call. If the user is logged in
// then get token details will return the token
// along with the profile details of the player
// The token should be passed to the game server
// with every game state change and be validated
// by your game server.
if (hasteClient) {
const details = hasteClient.getTokenDetails();
}, [hasteClient]);

return hasteClient ? (
// check and see if the user is authenticated
// and display the appropriate 'screen' based
// on that value. Using React state allows this
// page to perform dynamically.
<div className="App-header">
{isAuthenticated ? (
<div className="App-inner">
<p>Welcome {displayName}!</p>
<img src={picture} className="App-logo" alt="logo" />
onClick={() => {
// you should always provide a sign out
// option in your game client to allow
// the player to unauthenticate.
Sign Out
) : (
<div className="App-inner">
onClick={() => {
// login will perform a redirect to the Haste
// authentication client. Upon authentication
// the user will be redirected back to this page.
<p>Click the button to login.</p>
rel="noopener noreferrer"
Learn More about Haste SDK
) : (
<React.Fragment />

export default App;