Skip to content

Commit

Permalink
feat: extend remix-auth-oauth2
Browse files Browse the repository at this point in the history
  • Loading branch information
shayypy committed Apr 28, 2024
1 parent 5b7c294 commit 2716070
Show file tree
Hide file tree
Showing 8 changed files with 5,185 additions and 117 deletions.
22 changes: 0 additions & 22 deletions .eslintrc.js

This file was deleted.

24 changes: 8 additions & 16 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,14 @@ jobs:
run: npm run test -- --ci --coverage --maxWorkers=2

lint:
name: Linter
name: Biome
runs-on: ubuntu-latest
steps:
- name: Checkout repo
uses: actions/checkout@v2

- name: Use Node 14
uses: actions/setup-node@v1
- name: Checkout
uses: actions/checkout@v4
- name: Setup Biome
uses: biomejs/setup-biome@v2
with:
node-version: 14

- name: Install dependencies
uses: bahmutov/npm-install@v1

- name: Build
run: npm run build

- name: Lint
run: npm run lint
version: latest
- name: Run Biome
run: biome ci .
123 changes: 91 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,48 +1,107 @@
# Remix Auth - Strategy Template
# remix-auth-guilded

> A template for creating a new Remix Auth strategy.
remix-auth strategy for Authlink, an OAuth2 provider for Guilded. If you prefer Next.js, see [next-auth-guilded](https://github.com/GuildedAPI/next-auth-guilded).

If you want to create a new strategy for Remix Auth, you could use this as a template for your repository.
## Supported runtimes

The repo installs the latest version of Remix Auth and do the setup for you to have tests, linting and typechecking.
| Runtime | Has Support |
| ---------- | ----------- |
| Node.js ||
| Cloudflare ||

## How to use it
## How to use

1. In the `package.json` change `name` to your strategy name, also add a description and ideally an author, repository and homepage keys.
2. In `src/index.ts` change the `MyStrategy` for the strategy name you want to use.
3. Implement the strategy flow inside the `authenticate` method. Use `this.success` and `this.failure` to correctly send finish the flow.
4. In `tests/index.test.ts` change the tests to use your strategy and test it. Inside the tests you have access to `jest-fetch-mock` to mock any fetch you may need to do.
5. Once you are ready, set the secrets on Github
- `NPM_TOKEN`: The token for the npm registry
- `GIT_USER_NAME`: The git username you want the bump workflow to use in the commit.
- `GIT_USER_EMAIL`: The email you want the bump workflow to use in the commit.
### Create an Authlink application

## Scripts
Visit https://authlink.app/dev/apps, press "new", and connect your Guilded bot to finalize the application. Add a redirect URI and note down your client ID and secret (you will need to press "reset" to generate a secret). You may also configure a vanity code with preconfigured options, though beware that these can be overridden.

- `build`: Build the project for production using the TypeScript compiler (strips the types).
- `typecheck`: Check the project for type errors, this also happens in build but it's useful to do in development.
- `lint`: Runs ESLint against the source codebase to ensure it pass the linting rules.
- `test`: Runs all the test using Jest.
### Create session storage

## Documentations
```ts
// app/session.server.ts
import { createCookieSessionStorage } from "@remix-run/node";

To facilitate creating a documentation for your strategy, you can use the following Markdown
export const sessionStorage = createCookieSessionStorage({
cookie: {
name: "_session",
sameSite: "lax",
path: "/",
httpOnly: true,
secrets: ["s3cr3t"],
secure: process.env.NODE_ENV === "production",
},
});

```markdown
# Strategy Name
export const { getSession, commitSession, destroySession } = sessionStorage;
```

<!-- Description -->
### Create the strategy instance

```ts
// app/auth.server.ts
import { Authenticator } from "remix-auth";
import { GuildedStrategy, type GuildedUser } from "remix-auth-guilded";
import { sessionStorage } from "~/session.server";

export interface GuildedAuth {
id: string;
name: string;
avatar: string | null;
banner: string | null;
accessToken: string;
refreshToken: string;
}

export const auth = new Authenticator<DiscordAuth>(sessionStorage);

const guildedStrategy = new GuildedStrategy(
{
clientId: "YOUR_CLIENT_ID",
clientSecret: "YOUR_CLIENT_SECRET",
redirectUri: "https://example.com/auth/guilded/callback",
scope: ["identify"],
},
// OR { clientId, clientSecret, vanity: "..." }
async ({
accessToken,
refreshToken,
extraParams,
profile,
}): Promise<DiscordAuth> => {
// This package does not fetch the user for you
const user = await this.getUser(accessToken)

// This goes into your sessionStorage and is also returned by
// this method if successRedirect is not provided
return {
id: user.id,
name: user.name,
avatar: user.avatar,
banner: user.banner,
accessToken,
refreshToken,
};
},
);

auth.use(guildedStrategy);
```

## Supported runtimes
### Use in a loader

| Runtime | Has Support |
| ---------- | ----------- |
| Node.js | ✅ |
| Cloudflare | ✅ |
```ts
// app/routes/auth.guilded.callback.tsx
import type { LoaderFunction } from "@remix-run/node";
import { auth } from "~/auth.server";

<!-- If it doesn't support one runtime, explain here why -->
export const loader: LoaderFunction = ({ request }) => {
return auth.authenticate("guilded", request, {
successRedirect: "/dashboard",
failureRedirect: "/login",
});
};
```

## How to use
## Attribution

<!-- Explain how to use the strategy, here you should tell what options it expects from the developer when instantiating the strategy -->
```
Design elements & examples from [remix-auth-discord](https://github.com/JonnyBnator/remix-auth-discord).
15 changes: 15 additions & 0 deletions biome.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"$schema": "https://biomejs.dev/schemas/1.7.1/schema.json",
"organizeImports": {
"enabled": true
},
"linter": {
"enabled": true,
"rules": {
"recommended": true
}
},
"formatter": {
"indentStyle": "space"
}
}
27 changes: 14 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
{
"name": "remix-auth-strategy-template",
"version": "0.0.0",
"name": "remix-auth-guilded",
"version": "1.0.0",
"main": "./build/index.js",
"types": "./build/index.d.ts",
"repository": {
"url": "https://github.com/GuildedAPI/remix-auth-guilded"
},
"author": {
"name": "shay (shayypy)",
"url": "https://shay.cat"
},
"scripts": {
"build": "tsc --project tsconfig.json",
"typecheck": "tsc --project tsconfig.json --noEmit",
"lint": "eslint --ext .ts,.tsx src/",
"lint": "biome lint .",
"fix": "biome check --apply .",
"test": "jest --config=config/jest.config.ts --passWithNoTests",
"coverage": "npm run test -- --coverage"
},
Expand All @@ -31,27 +39,20 @@
"@babel/preset-env": "^7.14.1",
"@babel/preset-react": "^7.13.13",
"@babel/preset-typescript": "^7.13.0",
"@biomejs/biome": "^1.7.1",
"@remix-run/node": "^1.0.3",
"@remix-run/react": "^1.1.1",
"@remix-run/server-runtime": "^1.0.0",
"@types/jest": "^26.0.23",
"@typescript-eslint/eslint-plugin": "^4.23.0",
"@typescript-eslint/parser": "^4.23.0",
"babel-jest": "^26.6.3",
"eslint": "^7.26.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-jest": "^24.3.6",
"eslint-plugin-jest-dom": "^3.9.0",
"eslint-plugin-prettier": "^3.4.0",
"eslint-plugin-unicorn": "^32.0.1",
"jest": "^26.6.3",
"jest-fetch-mock": "^3.0.3",
"prettier": "^2.3.2",
"react": "^17.0.2",
"ts-node": "^9.1.1",
"typescript": "^4.3.5"
},
"dependencies": {
"remix-auth": "^3.0.0"
"remix-auth": "^3.0.0",
"remix-auth-oauth2": "^1.11.2"
}
}
Loading

0 comments on commit 2716070

Please sign in to comment.