@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 +60 -69
- package/index.d.ts +3 -0
- package/index.mjs +977 -0
- package/lib/auth.const.d.ts +16 -0
- package/lib/createAuth.d.ts +2 -0
- package/lib/keycloak.adapter.d.ts +14 -0
- package/lib/request.d.ts +13 -0
- package/lib/utils.d.ts +5 -0
- package/package.json +1 -2
- package/eslint.config.cjs +0 -21
- package/project.json +0 -13
- package/src/index.ts +0 -3
- package/src/lib/authentication.store.ts +0 -46
- package/src/lib/keycloak.adapter.ts +0 -55
- package/src/lib/request.store.ts +0 -126
- package/src/lib/vue-shims.d.ts +0 -7
- package/tsconfig.json +0 -23
- package/tsconfig.lib.json +0 -31
- package/tsconfig.spec.json +0 -28
- package/vite.config.ts +0 -61
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
|
|
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
|
-
|
|
29
|
+
## Enable authentication
|
|
21
30
|
|
|
22
|
-
|
|
31
|
+
Enable the authentication plugin in your `main.ts` file.
|
|
23
32
|
|
|
24
|
-
|
|
25
|
-
|
|
33
|
+
```typescript
|
|
34
|
+
import { createAuth } from '@ghentcdh/authentication-vue';
|
|
26
35
|
|
|
27
|
-
|
|
28
|
-
</script>
|
|
36
|
+
// Other app initialisation ...o
|
|
29
37
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
60
|
+
```typescript
|
|
61
|
+
import { createAuth } from '@ghentcdh/authentication-vue';
|
|
60
62
|
|
|
61
|
-
|
|
63
|
+
// Get the user
|
|
64
|
+
await getUser();
|
|
62
65
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
- KEYCLOAK_REALM=$KEYCLOAK_REALM
|
|
66
|
+
// Check if the user is authenticated
|
|
67
|
+
await isAuthenticated();
|
|
66
68
|
```
|
|
67
69
|
|
|
68
|
-
###
|
|
70
|
+
### Perform backend requests with the token
|
|
69
71
|
|
|
70
|
-
|
|
72
|
+
```vue
|
|
71
73
|
|
|
72
|
-
|
|
74
|
+
<script setup lang="ts">
|
|
75
|
+
import { useHttpRequest } from "@ghentcdh/authentication/vue";
|
|
73
76
|
|
|
74
|
-
|
|
77
|
+
const httpRequest = useHttpRequest();
|
|
75
78
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
})
|
|
79
|
-
|
|
80
|
-
}
|
|
79
|
+
httpRequest.post('/api/auth/login', {}).then(response => {
|
|
80
|
+
alert('login ok')
|
|
81
|
+
});
|
|
82
|
+
</script>
|
|
81
83
|
|
|
82
84
|
```
|
|
83
85
|
|
|
84
|
-
|
|
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
|
-
|
|
88
|
+
```typescript
|
|
100
89
|
|
|
90
|
+
```vue
|
|
101
91
|
|
|
102
|
-
|
|
103
|
-
|
|
92
|
+
<script setup lang="ts">
|
|
93
|
+
import { useHttpRequest } from "@ghentcdh/authentication/vue";
|
|
104
94
|
|
|
105
|
-
|
|
95
|
+
const httpRequest = useHttpRequest();
|
|
106
96
|
|
|
107
|
-
|
|
108
|
-
|
|
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
|
-
|
|
115
|
-
|
|
116
|
-
>
|
|
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