A template for an authentication service using oAuth2
Define the provider in providers
constant located in src/providers/constants.ts
Create a directory in src/providers
with the name defined in providers
. e.g. if in providers you have Facebook: 'facebook-provider'
the directory name will be facebook-provider
In the directory, you should have 2 files.
config.ts
- should default export 2 keys:
redirectPath
- With this exact namecallbackPath
- With this exact namestrategy.ts
- should default export the Strategy class, check the google example.In the auth.controller.ts
define the 2 routes and assign the @AuthMethod
attribute with the provider and method. e.g.
@AuthMethod(providers.Google, methods.Callback)
public authWithGoogle() {
throw new InternalServerErrorException();
}
@AuthMethod(providers.Google, methods.Authorize)
public loginWithGoogle() {
throw new InternalServerErrorException();
}
Key | Value |
---|---|
GOOGLE_CLIENT_ID | <google_client> |
GOOGLE_CLIENT_SECRET | <google_secret> |
LOGIN_ROUTE | /login |
HOME_ROUTE | / |
UUID_NAMESPACE | <uuid_v5_namespace> (It can be an uuidV4 generated) |
JWT_PRIVATE_KEY | <rsa_private_key> |
JWT_PUBLIC_KEY | <rsa_public_key> |
DATABASE_URL | postgresql://user:pass@host:port/db?schema=schema |
PROXY_IP | <ip_address> (The ip for the proxy) (OPTIONAL) |
For PROXY_IP
, it has to be the ip for the proxy that the app is used from,
e.g. 127.0.0.1
for a local proxy.
Generate the private key:
openssl genrsa -out private.key 3072
Extract the public key
openssl rsa -in private.pem -pubout -out public.pem
When saving the key in .env, remove the EOL (keep the key in a single line) and remove the prefix/suffix.