open-index/open-npm huggingface dataset:
npm package: @ghentcdh/authentication-api
Description: GhentCdh Keycloak libraries for authentication
Latest version: 0.6.9

GhentCdh Keycloak libraries for authentication


# Backend nestjs

## Install the libraries

```ssh
pnpm add @ghentcdh/auth/vue @ghentcdh/auth/backend
```

Add following environment variables to the `.env` file.

```
      - KEYCLOAK_HOST=$KEYCLOAK_HOST
      - KEYCLOAK_REALM=$KEYCLOAK_REALM
```

### Secure requests

Add the module to your module.ts imports

```typescript

import {AuthenticationApiModule} from "@ghentcdh/authentication-api";

@Module({
    imports: [AuthenticationApiModule],
})
export class MyModule {
}

```

Extend the `GhentCdhGuard` decorator to secure your routes



```typescript
import { Injectable } from '@nestjs/common';
import { GhentCdhGuard } from '@ghentcdh/authentication-api';
import { HttpService } from '@nestjs/axios';
import { ConfigService } from '@nestjs/config';

@Injectable()
export class MyGuard extends GhentCdhGuard {
  constructor(_httpService: HttpService, _configService: ConfigService) {
    super(_httpService, _configService);
  }
}

```

Use the guard in your controller

```typescript
import {MyGuard} from "./my.guard";

@Controller()
export class MyController {
    @UseGuards(MyGuard)
    @Post('/secure')
    async securePath(@User() user: any, @Request() req: any) {
        return user;
    }
}
```

The `@User()` decorator will give you the user object from the keycloak token.


> TODO list
> - [ ] Add roles decorator and implement logic `@GhentCdhRoles(['admin'])`

## Development environment

Make sure the following is added to your hosts file, if you are running a local keycloak instance. Internally the docker
containers cannot connect to the localhost:8080 port, so a hook is needed in terms of host rewrite.

```sh
echo "127.0.0.1 authentication\n" | sudo tee -a /etc/hosts
```

## Setup docker

> TODO describe me
npm package: @ghentcdh/authentication-vue
Description: GhentCdh Keycloak libraries for authentication
Latest version: 0.6.9

GhentCdh Keycloak libraries for authentication

# Vue frontend

## Install the libraries

```ssh
pnpm add @ghentcdh/auth/vue @ghentcdh/auth/backend
```

# Use the GhentCDH keycloak libraries for authentication

## Install the libraries

```ssh
pnpm add @ghentcdh/auth/vue @ghentcdh/auth/backend
```

## Frontend vuejs

Add following environment variables to the `.env` file.

```bash
      - VITE_KEYCLOAK_HOST=$KEYCLOAK_HOST
      - VITE_KEYCLOAK_REALM=$KEYCLOAK_REALM
      - VITE_KEYCLOAK_CLIENT_ID=$KEYCLOAK_CLIENT_ID
```

## Enable authentication

Enable the authentication plugin in your `main.ts` file.

```typescript
import { createAuth } from '@ghentcdh/authentication-vue';

// Other app initialisation ...o

app.use(
  createAuth({
    keycloak: {
      realm: import.meta.env.VITE_KEYCLOAK_REALM,
      url: import.meta.env.VITE_KEYCLOAK_HOST,
      clientId: import.meta.env.VITE_KEYCLOAK_CLIENT_ID,
    },
  }),
);

```

Create auth options 

| Option    | Default | Functionality                                                |
|-----------|---------|--------------------------------------------------------------|
| skipAuth  | false   | Skip authentication by default if using HttpRequest function |

## Functions

### User related functions

```typescript
import { createAuth } from '@ghentcdh/authentication-vue';

// Get the user
await getUser();

// Check if the user is authenticated
await isAuthenticated();
```

### Perform backend requests with the token

```vue

<script setup lang="ts">
  import { useHttpRequest } from "@ghentcdh/authentication/vue";

  const httpRequest = useHttpRequest();

  httpRequest.post('/api/auth/login', {}).then(response => {
    alert('login ok')
  });
</script>

```

Additional a parameter can be provided if the authentication should be skipped.

```typescript 

```vue

<script setup lang="ts">
  import { useHttpRequest } from "@ghentcdh/authentication/vue";

  const httpRequest = useHttpRequest();

  httpRequest.post('/api/skip-auth', {}, {skipAuth: true}).then(response => {
    alert('login ok')
  });
</script>

```

> TODO list
> - [ ] Add roles guard to see if routes or parts of the application can be accessed by that user
> - [ ] Add whitelisted routes
> - [ ] Add logout functionality