https://github.com/thesmartmonkey/simple-passwordless-auth
A lightweight and simple library for passwordless authentication. Built to help you get things done quickly and securely
https://github.com/thesmartmonkey/simple-passwordless-auth
Last synced: over 1 year ago
JSON representation
A lightweight and simple library for passwordless authentication. Built to help you get things done quickly and securely
- Host: GitHub
- URL: https://github.com/thesmartmonkey/simple-passwordless-auth
- Owner: TheSmartMonkey
- License: mit
- Created: 2025-01-05T10:26:06.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-01-13T22:27:33.000Z (over 1 year ago)
- Last Synced: 2025-01-21T16:36:17.356Z (over 1 year ago)
- Language: TypeScript
- Size: 225 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# simple-passwordless-auth
A lightweight and simple library for passwordless authentication. Built to help you get things done quickly and securely

## Installation
```sh
npm i simple-passwordless-auth
```
## Features
- [x] Google OAuth2
- [x] Passwordless authentication with a Drizzle ORM integration (you can also use your own database / ORM)
## Simple examples
### Passwordless authentication
```ts
import { login, validateCode } from 'simple-passwordless-auth';
// Login with email
let authCode;
await login(
email,
(user) => {
console.log('getUserByEmailAndUpdateUserIfExist');
authCode = user.authCode;
console.log({ authCode });
return Promise.resolve({} as UserDao);
},
() => {
console.log('createUser');
return Promise.resolve();
},
() => {
console.log('sendEmailWithVerificationCode');
return Promise.resolve();
},
);
// Validate the auth code
const isValid = await validateCode(process.env.JWT_SECRET ?? '', email, authCode, () => {
console.log('getUserByEmail');
return Promise.resolve({} as UserDao);
});
```
### Google OAuth2
```ts
import { initGoogleOAuth2Client } from 'simple-passwordless-auth';
const googleClient = initGoogleOAuth2Client(
process.env.GOOGLE_CLIENT_ID,
process.env.GOOGLE_CLIENT_SECRET,
process.env.GOOGLE_REDIRECT_URL,
);
// Redirect user to googleAuthUrl
const googleAuthUrl = getGoogleAuthUrl(googleClient);
// Handle the callback in a separate route
const userInfo = await handleGoogleCallback(googleClient, code);
```
#### Setup Google OAuth2
1. Go to [Google Cloud Console](https://console.cloud.google.com/)
2. Create a new project
3. Enable the Google OAuth2 API
4. Init a new OAuth consent screen
5. Create credentials for OAuth2 client ID and secret (copy the client ID and secret to `initGoogleOAuth2Client()`)
6. Set the redirect URL (example: `http://localhost:3000/auth/google/callback`)
## Rich example
See [debug/main.ts](debug/main.ts) for a complete example
### Contributing
See [CONTRIBUTING.md](CONTRIBUTING.md) for more information