Skip to content

Commit 870d7b1

Browse files
committed
Implemented useCurrentUser() now has isRole() and isaRole() #202
1 parent 2367a73 commit 870d7b1

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

app/composables/ving/useCurrentUser.mjs

+17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import ua from 'ua-parser-js';
22
import { isUndefined } from '#ving/utils/identify.mjs';
3+
import { RoleOptions } from '#ving/schema/schemas/User.mjs';
34

45
export const useCurrentUser = () => useVingRecord({
56
id: 'currentUser',
@@ -32,6 +33,22 @@ export const useCurrentUser = () => useVingRecord({
3233
return response;
3334
},
3435

36+
isRole(role) {
37+
if (role in this.props) {
38+
return this.props[role] || this.props.admin || false;
39+
}
40+
return false;
41+
},
42+
43+
isaRole(roles) {
44+
for (const role of roles) {
45+
if (this.isRole(role)) {
46+
return true;
47+
}
48+
}
49+
return false;
50+
},
51+
3552
async sendVerifyEmail(redirectAfter) {
3653
const parser = new ua(navigator.userAgent);
3754
const response = await useRest(this.links?.self?.href + '/send-verify-email', {

ving/docs/change-log.md

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ outline: deep
1313
* Implemented: apis should use plurals #183
1414
* NOTE: rest endpoints and pages use plurals like /users instead of /user now. This is a breaking change. Update your APIs and pages to use plurals.
1515
* Implemented: page generator should link to parent objects in the statistics section #158
16+
* Implemented useCurrentUser() now has isRole() and isaRole() #202
1617

1718
### 2025-01-12
1819
* Removed pulumi from the project.

ving/docs/subsystems/ui.md

+22
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,28 @@ It also triggers 2 window events for when the user logs in or out.
377377
});
378378
```
379379

380+
#### Extended Functionality
381+
`useCurrentUser()` as all the same functionality as the `useVingRecord()` composable, but also has a few extra methods:
382+
383+
##### login()
384+
Logs in a user.
385+
386+
##### logout()
387+
Logs out a user.
388+
389+
##### isRole(role)
390+
Returns `true` if the current user has the specified role.
391+
392+
##### isaRole(roles)
393+
Returns `true` if the current user has any of the specified roles.
394+
395+
##### sendVerifyEmail(redirectAfter)
396+
Sends a verification email to the user. If `redirectAfter` is passed in, then the user will be redirected to that URL after they verify their email.
397+
398+
##### verifyEmail(verify)
399+
Verifies a user's email. If `verify` is passed in, then the user will be redirected to that URL after they verify their email.
400+
401+
380402

381403
### useMessageBus()
382404
Connects the browser to the server's [message bus](messagebus). It establishes a connection between your browser and the server, so it needs to be installed in an `onMounted()` handler in your layouts.

0 commit comments

Comments
 (0)