Skip to main content

Authentication - Regular Web App

Example Authentication - Regular Web App

Details

This example focuses on performing a game client authentication using a standard request and response web architecture. The example utilizes Express as the framework 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 you will need to reach out to us on Discord for the client id and secret. Once that is complete please run the following commands:

git clone git@github.com:hastearcade/haste-sdk.git
cd haste-sdk/examples/authentication/regular-web-app/
npm install
cp .env-sample .env
npm start

app.js

import createError from "http-errors";
import express, { json, urlencoded, static as stc } from "express";
import favicon from "serve-favicon";
import { join } from "path";
import { config } from "dotenv";
import cookieParser from "cookie-parser";
import indexRouter from "./routes/index.js";
import usersRouter from "./routes/user.js";

import session from "express-session";
import passport from "passport";
import {
hasteAuthRoutes,
hasteUserInViews,
HasteStrategy,
} from "@hastearcade/haste-express";

// configure the dotenv library
config();

// configure passport to use the HasteStrategy provided by haste-express package
passport.use(HasteStrategy.initialize());

// You can use this section to keep a smaller payload
passport.serializeUser(function (user, done) {
done(null, user);
});

passport.deserializeUser(function (user, done) {
done(null, user);
});

// initialize session details for express + passport
const sess = {
secret: "shhhh", // this would change in a real application
resave: false,
saveUninitialized: true,
cookie: {},
};

const app = express();

app.use(cookieParser());
app.use(session(sess));
app.use(passport.initialize());
app.use(passport.session());

// view engine setup
app.set("views", join(".", "views"));
app.set("view engine", "jade");
app.use(favicon("." + "/public/images/favicon.ico"));

app.use(json());
app.use(urlencoded({ extended: false }));
app.use(stc(join(".", "public")));

// Important the following 2 lines are critical for integration and
// provide the callback routes and views for authentication.
app.use(hasteUserInViews());
app.use("/", hasteAuthRoutes);

app.use("/", indexRouter);
app.use("/", usersRouter);

// catch 404 and forward to error handler
app.use(function (req, res, next) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
next(createError(404));
});

// error handler
app.use(function (err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get("env") === "development" ? err : {};

// render the error page
res.status(err.status || 500);
res.render("error");
});

export default app;