Skip to content

Commit

Permalink
feat: implement OAuth2 with GitHub as Identity Provider on fastify pr…
Browse files Browse the repository at this point in the history
…oject
  • Loading branch information
이은상 committed Dec 27, 2024
1 parent 07ab3a5 commit d7add19
Show file tree
Hide file tree
Showing 9 changed files with 178 additions and 0 deletions.
23 changes: 23 additions & 0 deletions 7_auth/7_2_fastify/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Getting Started with [Fastify-CLI](https://www.npmjs.com/package/fastify-cli)
This project was bootstrapped with Fastify-CLI.

## Available Scripts

In the project directory, you can run:

### `npm run dev`

To start the app in dev mode.\
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.

### `npm start`

For production mode

### `npm run test`

Run the test cases.

## Learn More

To learn Fastify, check out the [Fastify documentation](https://fastify.dev/docs/latest/).
41 changes: 41 additions & 0 deletions 7_auth/7_2_fastify/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict'

const path = require('node:path')
const AutoLoad = require('@fastify/autoload')

// Pass --options via CLI arguments in command to enable these options.
const options = {}

module.exports = async function (fastify, opts) {
// Place here your custom code!
fastify.decorate('config', {
redisHost: process.env.REDIS_HOST || '127.0.0.1',
clientId: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
callbackUri: process.env.CALLBACK_URI || 'http://localhost:3000/callback',
frontendHost: process.env.FRONTEND_HOST || 'localhost',
frontendPort: process.env.FRONTEND_PORT || 4173,
jwtSecret: process.env.JWT_SECRET || 'your_jwt_secret',
});

console.log(fastify.config);

// Do not touch the following lines

// This loads all plugins defined in plugins
// those should be support plugins that are reused
// through your application
fastify.register(AutoLoad, {
dir: path.join(__dirname, 'plugins'),
options: Object.assign({}, opts)
})

// This loads all plugins defined in routes
// define your routes in one of these
fastify.register(AutoLoad, {
dir: path.join(__dirname, 'routes'),
options: Object.assign({}, opts)
})
}

module.exports.options = options
29 changes: 29 additions & 0 deletions 7_auth/7_2_fastify/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "7_2_fastify",
"version": "1.0.0",
"description": "This project was bootstrapped with Fastify-CLI.",
"main": "app.js",
"directories": {
"test": "test"
},
"scripts": {
"test": "node --test test/**/*.test.js",
"start": "fastify start -l info app.js",
"dev": "fastify start -w -l info -P app.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@fastify/autoload": "^6.0.0",
"@fastify/cookie": "^11.0.1",
"@fastify/oauth2": "^8.1.0",
"@fastify/redis": "^7.0.1",
"@fastify/sensible": "^6.0.0",
"axios": "^1.7.9",
"fastify": "^5.0.0",
"fastify-cli": "^7.2.0",
"fastify-plugin": "^5.0.0",
"jsonwebtoken": "^9.0.2"
}
}
16 changes: 16 additions & 0 deletions 7_auth/7_2_fastify/plugins/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Plugins Folder

Plugins define behavior that is common to all the routes in your
application. Authentication, caching, templates, and all the other cross
cutting concerns should be handled by plugins placed in this folder.

Files in this folder are typically defined through the
[`fastify-plugin`](https://github.com/fastify/fastify-plugin) module,
making them non-encapsulated. They can define decorators and set hooks
that will then be used in the rest of your application.

Check out:

* [The hitchhiker's guide to plugins](https://fastify.dev/docs/latest/Guides/Plugins-Guide/)
* [Fastify decorators](https://fastify.dev/docs/latest/Reference/Decorators/).
* [Fastify lifecycle](https://fastify.dev/docs/latest/Reference/Lifecycle/).
14 changes: 14 additions & 0 deletions 7_auth/7_2_fastify/plugins/sensible.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict'

const fp = require('fastify-plugin')

/**
* This plugins adds some utilities to handle http errors
*
* @see https://github.com/fastify/fastify-sensible
*/
module.exports = fp(async function (fastify, opts) {
fastify.register(require('@fastify/sensible'), {
errorHandler: false
})
})
12 changes: 12 additions & 0 deletions 7_auth/7_2_fastify/plugins/support.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict'

const fp = require('fastify-plugin')

// the use of fastify-plugin is required to be able
// to export the decorators to the outer scope

module.exports = fp(async function (fastify, opts) {
fastify.decorate('someSupport', function () {
return 'hugs'
})
})
29 changes: 29 additions & 0 deletions 7_auth/7_2_fastify/routes/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Routes Folder

Routes define the pathways within your application.
Fastify's structure supports the modular monolith approach, where your
application is organized into distinct, self-contained modules.
This facilitates easier scaling and future transition to a microservice architecture.
In the future you might want to independently deploy some of those.

In this folder you should define all the routes that define the endpoints
of your web application.
Each service is a [Fastify
plugin](https://fastify.dev/docs/latest/Reference/Plugins/), it is
encapsulated (it can have its own independent plugins) and it is
typically stored in a file; be careful to group your routes logically,
e.g. all `/users` routes in a `users.js` file. We have added
a `root.js` file for you with a '/' root added.

If a single file becomes too large, create a folder and add a `index.js` file there:
this file must be a Fastify plugin, and it will be loaded automatically
by the application. You can now add as many files as you want inside that folder.
In this way you can create complex routes within a single monolith,
and eventually extract them.

If you need to share functionality between routes, place that
functionality into the `plugins` folder, and share it via
[decorators](https://fastify.dev/docs/latest/Reference/Decorators/).

If you're a bit confused about using `async/await` to write routes, you would
better take a look at [Promise resolution](https://fastify.dev/docs/latest/Reference/Routes/#promise-resolution) for more details.
7 changes: 7 additions & 0 deletions 7_auth/7_2_fastify/routes/example/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict'

module.exports = async function (fastify, opts) {
fastify.get('/', async function (request, reply) {
return 'this is an example'
})
}
7 changes: 7 additions & 0 deletions 7_auth/7_2_fastify/routes/root.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict'

module.exports = async function (fastify, opts) {
fastify.get('/', async function (request, reply) {
return { root: true }
})
}

0 comments on commit d7add19

Please sign in to comment.