# sveltekit-passport-oauth2

> Easy way to use passport (with OAuth2 authentication) in your svelte-kit projects

Latest version **0.0.6** (published 2021-10-24) · ISC license · 0 weekly downloads

## Install

```sh
npm install sveltekit-passport-oauth2
pnpm add sveltekit-passport-oauth2
yarn add sveltekit-passport-oauth2
bun add sveltekit-passport-oauth2
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.6 |
| Published | 2021-10-24 |
| First published | 2021-09-10 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 4 |
| Unpacked size | 39.1 KB |
| Known vulnerabilities | 0 (+2 in 2 direct dependencies) |
| Install scripts | no |
| GitHub stars | 20 |
| Author | Fabiohvp |
| Maintainers | fabiohvp |
| Keywords | SvelteKit, Svelte-Kit, Passport, OAuth, Google Auth, Facebook Auth |

## Links

- npm: https://www.npmjs.com/package/sveltekit-passport-oauth2
- Repository: https://github.com/fabiohvp/sveltekit-passport-oauth2
- Homepage: https://github.com/fabiohvp/sveltekit-passport-oauth2#readme
- Issues: https://github.com/fabiohvp/sveltekit-passport-oauth2/issues
- npm.io page: https://npm.io/package/sveltekit-passport-oauth2

## Dependencies (4)

- [cookie](https://npm.io/package/cookie.md) ^0.4.1
- [ts-lint](https://npm.io/package/ts-lint.md) ^4.5.1
- [passport](https://npm.io/package/passport.md) ^0.4.1
- [@lukeed/uuid](https://npm.io/package/@lukeed/uuid.md) ^2.0.0

## Alternatives

- [@pothos/plugin-authz](https://npm.io/package/@pothos/plugin-authz.md) — 12.4K weekly downloads
- [@bounded-sh/client](https://npm.io/package/@bounded-sh/client.md) — 3.2K weekly downloads
- [@luigi-project/plugin-auth-oauth2](https://npm.io/package/@luigi-project/plugin-auth-oauth2.md) — 2.3K weekly downloads
- [@nocobase/plugin-verification](https://npm.io/package/@nocobase/plugin-verification.md) — 2.0K weekly downloads
- [escher-request](https://npm.io/package/escher-request.md) — 545 weekly downloads

## Recent versions

- 0.0.6 (latest) — 2021-10-24
- 0.0.5 — 2021-09-19
- 0.0.4 — 2021-09-14
- 0.0.3 — 2021-09-14
- 0.0.2 — 2021-09-10
- 0.0.1 — 2021-09-10

## README

# sveltekit-passport-oauth2

Easy way to use passport (with OAuth2 authentication) in your svelte-kit projects

```
npm install sveltekit-passport-oauth2
```

## Configure hooks.ts

The example bellow uses Google but it should work for other strategies (also tested with facebook).

```ts
import {
	ConfigurePassportOAuth2,
	OAuthHandleInput,
	OAuthCreateCookie,
	DefaultCookieName
} from 'sveltekit-passport-oauth2';

ConfigurePassportOAuth2([
	{
		strategy: new GoogleStrategy(
			{
				callbackURL: 'http://localhost:3000/auth/google/callback.json',
				clientID: 'use-your-google-client-id',
				clientSecret: 'use-your-google-client-secret',
				passReqToCallback: true
			},
			OAuthCreateCookie(/*cookieName = DefaultCookieName, cookieSettings?: CookieSettings*/)
		)
	}
]);
export const handle = sequence(addUserToRequest);

//this is optional (just an example of how to add the user info to request)
async function addUserToRequest({ request, resolve }: OAuthHandleInput) {
	const cookies = cookie.parse(request.headers.cookie || '');
	const cookieId = cookies[DefaultCookieName];
	if (cookieId) {
		request.locals = Database[cookieId]; //change this to retrieve from database
	}
	const response = await resolve(request);
	return response;
}

//this is optional (it will allow you to use session in .svelte file) like:
//<script lang="ts">
//	import {session} from '$app/stores';
//	session.subscribe(console.log) //this will print user data when authenticated
//</script>
export function getSession(request: ServerRequest<Locals>): Locals {
	return request.locals;
}
```

## Create authentication routes

```ts
//login.ts
//first parameter is the strategy name
//second paramater are passport options (usually you'll only use the "scope" property)
export const get = loginMethod('google', { scope: ['email', 'profile'] });

//callback.ts
//first parameter is the strategy name
//second parameter is a callback in case you want to redirect. If you don't pass a callback the response will
//be only headers to set cookie to maxAge={DefaultCookieSettings.maxAge} and an empty body
export const get = callbackMethod(
	'google',
	(request: ServerRequest<OAuthSession>, response: OAuthResponse) => {
		if (request.locals.authenticated) {
			Database[request.locals.cookieId] = request.locals; //change this to save in database
			return response.redirect('/');
		}
		return response.redirect('/about');
	}
);

//logout.ts
//first parameter is the cookieName, if you pass 'undefined' it will use the {DefaultCookieName}
//second parameter is a callback in case you want to redirect, if you don't pass a callback the response will
//be only headers to set cookie to maxAge=0 and an empty body
export const get = logoutMethod(undefined, (request, response) => {
	return response.redirect('/');
});
```

---
_Source: https://npm.io/package/sveltekit-passport-oauth2 · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
