scratchauth.js
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

ScratchAuth.JS

A typed API wrapper and middleware for Scratch Auth by Looky1173

To install, use:

$   npm install scratchauth.js

Express Middleware Example

const ScratchAuth = require("scratchauth.js");
const express = require("express");

const app = express();

app.get(
  "/auth",
  ScratchAuth.middleware.authUrl(
    "http://localhost:3000/auth/scratch",
    "ScratchAuth.js Test"
  )
);

const ScratchAuthMiddleware = ScratchAuth.middleware.validateAuth({
  redirectUrl: "http://localhost:3000/auth/scratch", // IMPLEMENT THIS FOR SECURITY
  onFail: (req, res, next) => {
    res.status(401).json({ code: 401, message: "Unauthorized" });
  },
});

app.get("/auth/scratch", ScratchAuthMiddleware, (req, res) => {
  if (req.scratch) {
    res.send(`Welcome, ${req.scratch.username}`);
  }
});

app.listen(3000, () => {
  console.log("Server ready!");
});

Functions

const ScratchAuth = require("scratchauth.js");
// Gives the encoded url for authenticating the user.
ScratchAuth.makeAuthUrl("url", "Example Application");
// Gives the response for validating the auth token you have.
ScratchAuth.validateAuth("912...39b");

Middleware Functions

const ScratchAuth = require("scratchauth.js");
// Makes the auth url and redirects any user to the result URL.
ScratchAuth.middleware.authUrl("url", "Example Application");
// Validates the auth token passed in the request.
ScratchAuth.middleware.validateAuth({
  fail: (req, res, next) => {},
  redirectUrl: "http://localhost:3000/", // use this for security
});

Express Middleware

To get the user from express, use req.scratch to get the scratch user info (if valid, you should check if it exists. at all)

Readme

Keywords

none

Package Sidebar

Install

npm i scratchauth.js

Weekly Downloads

0

Version

1.0.0

License

ISC

Unpacked Size

11.8 kB

Total Files

10

Last publish

Collaborators

  • dogeplayz