Skip to content

Commit

Permalink
chore: add federated and github documentation adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
tnramalho committed Sep 30, 2024
1 parent efdab9f commit d4b6d7d
Show file tree
Hide file tree
Showing 2 changed files with 262 additions and 105 deletions.
117 changes: 14 additions & 103 deletions packages/nestjs-auth-github/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,10 @@ Authenticate requests using GitHub OAuth2
1. [Tutorials](#tutorials)
- [Introduction](#introduction)
- [Getting Started with GitHub Authentication](#getting-started-with-github-authentication)
- [Step 1: Create User Entity](#step-1-create-user-entity)
- [Step 2: Create Federated Entity](#step-2-create-federated-entity)
- [Step 3: Implement FederatedUserLookupServiceInterface](#step-3-implement-federateduserlookupserviceinterface)
- [Step 4: Implement FederatedUserMutateServiceInterface](#step-4-implement-federatedusermutateserviceinterface)
- [Step 5: Implement IssueTokenServiceInterface](#step-5-implement-issuetokenserviceinterface)
- [Step 6: Environment Variables](#step-6-environment-variables)
- [Step 7: Configure the Module](#step-7-configure-the-module)
- [Step 1: Associate User Entity to Federated Entity](#step-1-associate-user-entity-to-federated-entity)
- [Step 2: Associate Federated Entity to User Entity](#step-2-associate-federated-entity-to-user-entity)
- [Step 3: Environment Variables](#step-3-environment-variables)
- [Step 4: Configure the Module](#step-4-configure-the-module)
2. [Testing the GitHub Authentication Flow](#testing-the-github-authentication-flow)
3. [How-To Guides](#how-to-guides)
- [Customizing the Issue Token Service](#customizing-the-issue-token-service)
Expand All @@ -47,7 +44,10 @@ official documentation](https://docs.github.com/en/apps/oauth-apps/building-oaut

## Getting Started with GitHub Authentication

### Step 1: Create User Entity
**Prerequisite:** Github module has a dependency on `@concepta/nestjs-federated`. Ensure you have implemented the `FederatedModule` before proceeding. Refer to

Check notice on line 47 in packages/nestjs-auth-github/README.md

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

packages/nestjs-auth-github/README.md#L47

Expected: 0 or 2; Actual: 1

Check notice on line 47 in packages/nestjs-auth-github/README.md

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

packages/nestjs-auth-github/README.md#L47

Expected: 80; Actual: 160
the [Federated API Documentation](https://www.rockets.tools/reference/rockets/nestjs-federated/README) for more details.

Check notice on line 48 in packages/nestjs-auth-github/README.md

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

packages/nestjs-auth-github/README.md#L48

Expected: 80; Actual: 120

### Step 1: Associate User Entity to Federated Entity

First, let's create the `UserEntity`:

Expand All @@ -68,9 +68,9 @@ export class UserEntity {
}
```

### Step 2: Create Federated Entity
### Step 2: Associate Federated Entity to User Entity

Next, create the `FederatedEntity`:
Next, associate the `UserEntity` to the `FederatedEntity`:

```ts
import { Entity, ManyToOne } from 'typeorm';
Expand All @@ -84,97 +84,7 @@ export class FederatedEntity extends FederatedSqliteEntity {
}
```

### Step 3: Implement FederatedUserLookupServiceInterface

Create a service that implements `FederatedUserLookupServiceInterface`:

```ts
// user.mock.ts
export const mockUser = {
id: 'abc',
email: '[email protected]',
username: '[email protected]',
}
```

```ts
// user-lookup.service.ts
import { Injectable } from '@nestjs/common';
import { ReferenceEmail } from '@concepta/ts-core';
import { FederatedUserLookupServiceInterface } from '@concepta/nestjs-federated';
import { mockUser } from './user.mock';


@Injectable()
export class UserLookupServiceFixture implements FederatedUserLookupServiceInterface {
async byId(id: string): ReturnType<FederatedUserLookupServiceInterface['byId']> {
if (id === mockUser.id) {
return mockUser;
} else {
throw new Error();
}
}

async byEmail(email: ReferenceEmail): ReturnType<FederatedUserLookupServiceInterface['byEmail']> {
return email === mockUser.email ? mockUser : null;
}
}
```

### Step 4: Implement FederatedUserMutateServiceInterface

Create a service that implements `FederatedUserMutateServiceInterface`:

```ts
// user-mutate.service.ts
import { FederatedCredentialsInterface, FederatedUserMutateServiceInterface } from '@concepta/nestjs-federated';
import { FederatedUserMutateInterface } from '@concepta/nestjs-federated/dist/interfaces/federated-user-mutate.interface';
import { Injectable } from '@nestjs/common';
import { mockUser } from './user.mock';

@Injectable()
export class UserMutateServiceFixture implements FederatedUserMutateServiceInterface {
async create(_object: FederatedUserMutateInterface): Promise<FederatedCredentialsInterface> {
return mockUser;
}
}
```

### Step 5: Implement IssueTokenServiceInterface

Create a service that implements `IssueTokenServiceInterface`:

```ts
// issue-token.service.ts
import { JwtSignOptions } from '@nestjs/jwt';
import { AuthenticationResponseInterface } from '@concepta/ts-common';
import { IssueTokenServiceInterface } from '@concepta/nestjs-authentication';

export class GitHubIssueTokenService implements IssueTokenServiceInterface {
async responsePayload(_id: string): Promise<AuthenticationResponseInterface> {
return {
accessToken: 'my_access_token',
refreshToken: 'my_refresh_token',
};
}

async accessToken(
_payload: string | object | Buffer,
_options?: JwtSignOptions | undefined,
): Promise<string> {
return 'my_access_token';
}

async refreshToken(
_payload: string | object | Buffer,
_options?: JwtSignOptions | undefined,
): Promise<string> {
return 'my_refresh_token';
}
}
```

### Step 6: Environment Variables
### Step 3: Environment Variables

`AuthGithubModule` will automatically look for predefined environment
variables, to keep sensitive information secure, use environment variables for
Expand Down Expand Up @@ -209,7 +119,7 @@ the `@nestjs/config` package:
export class AppModule {}
```

### Step 7: Configure the Module
### Step 4: Configure the Module

Finally, set up the module configuration:

Expand Down Expand Up @@ -447,7 +357,8 @@ AuthGithubModule.forRoot({

If you need to customize, how to get the user information, or how to create
the user, you will need to update the federated options. Please refer for
the `nestjs-federated` documentation.
the [Federated API Documentation](https://www.rockets.tools/reference/rockets/nestjs-federated/README)
documentation.

## Reference

Expand Down
Loading

0 comments on commit d4b6d7d

Please sign in to comment.