Skip to content

Commit 3e5b93e

Browse files
ErikCHErik Hanchett
andauthored
feat: Added Auth Status (#1804)
* Added Auth Check * Added changeset * Changed to authStatus * Updated changeset * Fixed tests * Updated text based on code review * Changed pending to configuring * Changed comments Co-authored-by: Erik Hanchett <[email protected]>
1 parent 957312e commit 3e5b93e

File tree

12 files changed

+153
-11
lines changed

12 files changed

+153
-11
lines changed

.changeset/spicy-queens-happen.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@aws-amplify/ui-angular': minor
3+
'@aws-amplify/ui': minor
4+
'@aws-amplify/ui-vue': minor
5+
---
6+
7+
New authStatus feature, to check if a user is authenticated or not

docs/src/pages/components/authenticator/advanced/current-route.angular.mdx

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,30 @@ You can use `AuthenticatorService` to access `route` string that represents the
2626
<amplify-authenticator></amplify-authenticator>
2727
</ng-container>
2828
```
29+
30+
#### Authentication Check
31+
32+
If you just need to check if you're authenticated or not, you can use the more straightforward `AuthenticatorService` to access the `authStatus` string. The `authStatus` string can represent the following states:
33+
34+
- `configuring`
35+
- `authenticated`
36+
- `unauthenticated`
37+
38+
> The `configuring` state only occurs when the `Authenticator` is first loading.
39+
40+
```html
41+
<!-- Render loading if authStatus is still configuring -->
42+
<ng-container *ngIf="authenticator.authStatus === 'configuring'">
43+
Loading...
44+
</ng-container>
45+
46+
<!-- Only render this if there's an authenticated user -->
47+
<ng-container *ngIf="authenticator.authStatus === 'authenticated'">
48+
Welcome back!
49+
</ng-container>
50+
51+
<!-- Render sign-in screen otherwise with authenticator -->
52+
<ng-container *ngIf="authenticator.authStatus !== 'authenticated'">
53+
<amplify-authenticator></amplify-authenticator>
54+
</ng-container>
55+
```

docs/src/pages/components/authenticator/advanced/current-route.react.mdx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,29 @@ const App = () => {
2525
return route === 'authenticated' ? <Home /> : <Authenticator />;
2626
};
2727
```
28+
29+
#### Authentication Check
30+
31+
If you just need to check if you're authenticated or not, you can use the more straightforward `useAuthenticator` hook to access the `authStatus` string. The `authStatus` string can represent the following states:
32+
33+
- `configuring`
34+
- `authenticated`
35+
- `unauthenticated`
36+
37+
> The `configuring` state only occurs when the `Authenticator` is first loading.
38+
39+
```tsx{1,4-7}
40+
import { useAuthenticator } from '@aws-amplify/ui-react';
41+
42+
const App = () => {
43+
const { authStatus } = useAuthenticator(context => [context.authStatus]);
44+
45+
// Use the value of authStatus to decide which page to render
46+
return (
47+
<>
48+
{authStatus === 'configuring' && 'Loading...'}
49+
{authStatus !== 'authenticated' ? <Authenticator /> : <Home />}
50+
</>
51+
);
52+
};
53+
```

docs/src/pages/components/authenticator/advanced/current-route.vue.mdx

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,30 @@ You can use `useAuthenticator` composable to access `route` string that represen
2828
</template>
2929
</template>
3030
```
31+
32+
#### Authentication Check
33+
34+
If you just need to check if you're authenticated or not, you can use the more straightforward `useAuthenticator` composable to access the `authStatus` string. The `authStatus` string can represent the following states:
35+
36+
- `configuring`
37+
- `authenticated`
38+
- `unauthenticated`
39+
40+
> The `configuring` state only occurs when the `Authenticator` is first loading.
41+
42+
```html{1,5-7}
43+
<script setup>
44+
import { Authenticator, useAuthenticator } from '@aws-amplify/ui-vue';
45+
const auth = useAuthenticator();
46+
</script>
47+
48+
<template>
49+
<authenticator></authenticator>
50+
<template v-if="auth.authStatus === 'configuring'">
51+
<button @click="auth.signOut">Loading...</button>
52+
</template>
53+
<template v-if="auth.authStatus === 'authenticated'">
54+
<button @click="auth.signOut">Sign out</button>
55+
</template>
56+
</template>
57+
```

examples/angular/src/pages/ui/components/authenticator/sign-up-with-email/sign-up-with-email.component.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{{ authStatus }}
12
<amplify-authenticator
23
[formFields]="formFields"
34
[services]="services"

examples/angular/src/pages/ui/components/authenticator/sign-up-with-email/sign-up-with-email.component.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { Component, OnInit } from '@angular/core';
22
import { Amplify, Auth, I18n } from 'aws-amplify';
33
import awsExports from './aws-exports';
4-
import { translations } from '@aws-amplify/ui-angular';
4+
import { AuthenticatorService, translations } from '@aws-amplify/ui-angular';
55

66
@Component({
77
selector: 'sign-up-with-email',
88
templateUrl: 'sign-up-with-email.component.html',
99
})
1010
export class SignUpWithEmailComponent implements OnInit {
11-
constructor() {
11+
constructor(public authenticator: AuthenticatorService) {
1212
Amplify.configure(awsExports);
1313
}
1414

@@ -33,6 +33,10 @@ export class SignUpWithEmailComponent implements OnInit {
3333
},
3434
};
3535

36+
get authStatus() {
37+
return this.authenticator.authStatus;
38+
}
39+
3640
services = {
3741
async handleSignUp(formData: Record<string, any>) {
3842
let { username, password, attributes } = formData;

examples/next/pages/ui/components/authenticator/sign-up-with-email/index.page.tsx

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import { Amplify, Auth, I18n } from 'aws-amplify';
22

3-
import { Authenticator, translations } from '@aws-amplify/ui-react';
3+
import {
4+
Authenticator,
5+
translations,
6+
useAuthenticator,
7+
View,
8+
} from '@aws-amplify/ui-react';
49
import '@aws-amplify/ui-react/styles.css';
510

611
import awsExports from './aws-exports';
@@ -25,6 +30,7 @@ I18n.putVocabulariesForLanguage('en', {
2530
});
2631

2732
export default function AuthenticatorWithEmail() {
33+
const { authStatus } = useAuthenticator((context) => [context.authStatus]);
2834
const services = {
2935
async handleSignUp(formData) {
3036
let { username, password, attributes } = formData;
@@ -40,12 +46,15 @@ export default function AuthenticatorWithEmail() {
4046
};
4147

4248
return (
43-
<Authenticator
44-
formFields={formFields}
45-
services={services}
46-
initialState="signUp"
47-
>
48-
{({ signOut }) => <button onClick={signOut}>Sign out</button>}
49-
</Authenticator>
49+
<>
50+
<View>{authStatus}</View>
51+
<Authenticator
52+
formFields={formFields}
53+
services={services}
54+
initialState="signUp"
55+
>
56+
{({ signOut }) => <button onClick={signOut}>Sign out</button>}
57+
</Authenticator>
58+
</>
5059
);
5160
}

examples/vue/src/pages/ui/components/authenticator/sign-up-with-email/index.vue

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
<script setup lang="ts">
22
import Amplify, { Auth, I18n } from 'aws-amplify';
3-
import { Authenticator, translations } from '@aws-amplify/ui-vue';
3+
import {
4+
Authenticator,
5+
translations,
6+
useAuthenticator,
7+
} from '@aws-amplify/ui-vue';
8+
import { toRefs } from 'vue';
49
import '@aws-amplify/ui-vue/styles.css';
510
import aws_exports from './aws-exports';
611
712
Amplify.configure(aws_exports);
813
14+
const { authStatus } = toRefs(useAuthenticator());
15+
916
const formFields = {
1017
confirmSignUp: {
1118
confirmation_code: {
@@ -39,6 +46,7 @@ const services = {
3946
</script>
4047

4148
<template>
49+
{{ authStatus }}
4250
<authenticator
4351
:services="services"
4452
:form-fields="formFields"

packages/angular/projects/ui-angular/src/lib/services/authenticator.service.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ export class AuthenticatorService implements OnDestroy {
7070
return this._facade?.route;
7171
}
7272

73+
public get authStatus() {
74+
return this._facade?.authStatus;
75+
}
76+
7377
public get user() {
7478
return this._facade?.user;
7579
}

packages/e2e/features/ui/components/authenticator/sign-up-with-email.feature

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,17 @@ Feature: Sign Up with Email
1212
And I don't see "Phone Number" as an input field
1313

1414
@angular @react @vue
15+
Scenario: Sign up with a new email & password and check auth message
16+
Given I intercept '{ "headers": { "X-Amz-Target": "AWSCognitoIdentityProviderService.SignUp" } }' with fixture "sign-up-with-email"
17+
And I see "unauthenticated"
18+
When I type a new "email"
19+
And I type my password
20+
And I confirm my password
21+
And I click the "Create Account" button
22+
And I see "authenticated"
23+
Then I see "Confirmation Code"
24+
25+
@angular @react @vue
1526
Scenario: Sign up with a new email & password and lowercase the email
1627
Given I intercept '{ "headers": { "X-Amz-Target": "AWSCognitoIdentityProviderService.SignUp" } }' with fixture "sign-up-with-email"
1728
When I type a new "email" with value "[email protected]"

0 commit comments

Comments
 (0)