@ghentcdh/authentication-vue 0.0.2-2 → 0.0.2-20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,116 +1,107 @@
1
+ GhentCdh Keycloak libraries for authentication
2
+
3
+ # Vue frontend
4
+
5
+ ## Install the libraries
6
+
7
+ ```ssh
8
+ pnpm add @ghentcdh/auth/vue @ghentcdh/auth/backend
9
+ ```
1
10
 
2
11
  # Use the GhentCDH keycloak libraries for authentication
3
12
 
4
13
  ## Install the libraries
5
14
 
6
15
  ```ssh
7
- pnpm add @ghentcdh/auth-vue @ghentcdh/auth-api
16
+ pnpm add @ghentcdh/auth/vue @ghentcdh/auth/backend
8
17
  ```
9
18
 
10
19
  ## Frontend vuejs
11
20
 
12
21
  Add following environment variables to the `.env` file.
13
22
 
14
- ```
23
+ ```bash
15
24
  - VITE_KEYCLOAK_HOST=$KEYCLOAK_HOST
16
25
  - VITE_KEYCLOAK_REALM=$KEYCLOAK_REALM
17
26
  - VITE_KEYCLOAK_CLIENT_ID=$KEYCLOAK_CLIENT_ID
18
27
  ```
19
28
 
20
- ### Check if a user is logged in
29
+ ## Enable authentication
21
30
 
22
- ```vue
31
+ Enable the authentication plugin in your `main.ts` file.
23
32
 
24
- <script setup lang="ts">
25
- import {useAuthenticationStore} from "@ghentcdh/authentication-vue";
33
+ ```typescript
34
+ import { createAuth } from '@ghentcdh/authentication-vue';
26
35
 
27
- const authenticationStore = useAuthenticationStore();
28
- </script>
36
+ // Other app initialisation ...o
29
37
 
30
- <template>
31
- <pre>user: {{ authenticationStore.user() }}</pre>
32
- </template>
38
+ app.use(
39
+ createAuth({
40
+ keycloak: {
41
+ realm: import.meta.env.VITE_KEYCLOAK_REALM,
42
+ url: import.meta.env.VITE_KEYCLOAK_HOST,
43
+ clientId: import.meta.env.VITE_KEYCLOAK_CLIENT_ID,
44
+ },
45
+ }),
46
+ );
33
47
 
34
48
  ```
35
49
 
36
- ### Perform backend requests with the token
37
-
38
- ```vue
39
-
40
- <script setup lang="ts">
41
- import {useHttpStore} from "@ghentcdh/authentication-vue";
42
-
43
- const httpStore = useHttpStore();
44
-
45
- httpStore.post('/api/auth/login', {}).then(response => {
46
- alert('login ok')
47
- });
48
- </script>
50
+ Create auth options
49
51
 
52
+ | Option | Default | Functionality |
53
+ |-----------|---------|--------------------------------------------------------------|
54
+ | skipAuth | false | Skip authentication by default if using HttpRequest function |
50
55
 
51
- ```
56
+ ## Functions
52
57
 
53
- > TODO list
54
- > - [ ] Add roles guard to see if routes or parts of the application can be accessed by that user
55
- > - [ ] Test if it's possible to have public routes
56
- > - [ ] Add a
57
- > - [ ] Add logout functionality
58
+ ### User related functions
58
59
 
59
- ## Backend nestjs
60
+ ```typescript
61
+ import { createAuth } from '@ghentcdh/authentication-vue';
60
62
 
61
- Add following environment variables to the `.env` file.
63
+ // Get the user
64
+ await getUser();
62
65
 
63
- ```
64
- - KEYCLOAK_HOST=$KEYCLOAK_HOST
65
- - KEYCLOAK_REALM=$KEYCLOAK_REALM
66
+ // Check if the user is authenticated
67
+ await isAuthenticated();
66
68
  ```
67
69
 
68
- ### Secure requests
70
+ ### Perform backend requests with the token
69
71
 
70
- Add the module to your module.ts imports
72
+ ```vue
71
73
 
72
- ```typescript
74
+ <script setup lang="ts">
75
+ import { useHttpRequest } from "@ghentcdh/authentication/vue";
73
76
 
74
- import {AuthenticationApiModule} from "@ghentcdh/authentication-api";
77
+ const httpRequest = useHttpRequest();
75
78
 
76
- @Module({
77
- imports: [AuthenticationApiModule],
78
- })
79
- export class MyModule {
80
- }
79
+ httpRequest.post('/api/auth/login', {}).then(response => {
80
+ alert('login ok')
81
+ });
82
+ </script>
81
83
 
82
84
  ```
83
85
 
84
- Use the `@GhentCdhGuard` decorator to secure your routes
85
-
86
- ```typescript
87
- import {GhentCdhGuard} from "@ghentcdh/authentication-api";
88
-
89
- @Controller()
90
- export class MyController {
91
- @UseGuards(GhentCdhGuard)
92
- @Post('/secure')
93
- async securePath(@User() user: any, @Request() req: any) {
94
- return user;
95
- }
96
- }
97
- ```
86
+ Additional a parameter can be provided if the authentication should be skipped.
98
87
 
99
- The `@User()` decorator will give you the user object from the keycloak token.
88
+ ```typescript
100
89
 
90
+ ```vue
101
91
 
102
- > TODO list
103
- > - [ ] Add roles decorator and implement logic `@GhentCdhRoles(['admin'])`
92
+ <script setup lang="ts">
93
+ import { useHttpRequest } from "@ghentcdh/authentication/vue";
104
94
 
105
- ## Development environment
95
+ const httpRequest = useHttpRequest();
106
96
 
107
- Make sure the following is added to your hosts file, if you are running a local keycloak instance. Internally the docker
108
- containers cannot connect to the localhost:8080 port, so a hook is needed in terms of host rewrite.
97
+ httpRequest.post('/api/skip-auth', {}, {skipAuth: true}).then(response => {
98
+ alert('login ok')
99
+ });
100
+ </script>
109
101
 
110
- ```sh
111
- echo "127.0.0.1 authentication\n" | sudo tee -a /etc/hosts
112
102
  ```
113
103
 
114
- ## Setup docker
115
-
116
- > TODO describe me
104
+ > TODO list
105
+ > - [ ] Add roles guard to see if routes or parts of the application can be accessed by that user
106
+ > - [ ] Add whitelisted routes
107
+ > - [ ] Add logout functionality
package/index.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ export * from './lib/createAuth';
2
+ export * from './lib/request';
3
+ export * from './lib/utils';