|
| 1 | +<template> |
| 2 | + <div class="auth-form container-background border-common" id="builder-id-form"> |
| 3 | + <div v-show="canShowAll"> |
| 4 | + <FormTitle :isConnected="isConnected">AWS Builder ID</FormTitle> |
| 5 | + |
| 6 | + <div v-if="stage === stages.START"> |
| 7 | + <div class="form-section"> |
| 8 | + <div> |
| 9 | + With AWS Builder ID, sign in for free without an AWS account. |
| 10 | + <a href="https://docs.aws.amazon.com/signin/latest/userguide/sign-in-aws_builder_id.html" |
| 11 | + >Read more.</a |
| 12 | + > |
| 13 | + </div> |
| 14 | + </div> |
| 15 | + |
| 16 | + <div class="form-section"> |
| 17 | + <button v-on:click="startSignIn()">Sign up or Sign in</button> |
| 18 | + </div> |
| 19 | + </div> |
| 20 | + |
| 21 | + <div v-if="stage === stages.WAITING_ON_USER"> |
| 22 | + <div class="form-section"> |
| 23 | + <div>Follow instructions...</div> |
| 24 | + </div> |
| 25 | + </div> |
| 26 | + |
| 27 | + <div v-if="stage === stages.CONNECTED"> |
| 28 | + <div class="form-section"> |
| 29 | + <div v-on:click="signout()" style="cursor: pointer; color: #75beff">Sign out</div> |
| 30 | + </div> |
| 31 | + </div> |
| 32 | + </div> |
| 33 | + </div> |
| 34 | +</template> |
| 35 | +<script lang="ts"> |
| 36 | +import { PropType, defineComponent } from 'vue' |
| 37 | +import BaseAuthForm from './baseAuth.vue' |
| 38 | +import FormTitle from './formTitle.vue' |
| 39 | +import { AuthStatus } from './shared.vue' |
| 40 | +import { WebviewClientFactory } from '../../../webviews/client' |
| 41 | +import { AuthWebview } from '../show' |
| 42 | +import authForms, { AuthFormId } from './types.vue' |
| 43 | +
|
| 44 | +const client = WebviewClientFactory.create<AuthWebview>() |
| 45 | +
|
| 46 | +/** Where the user is currently in the builder id setup process */ |
| 47 | +export const stages = { |
| 48 | + START: 'START', |
| 49 | + WAITING_ON_USER: 'WAITING_ON_USER', |
| 50 | + CONNECTED: 'CONNECTED', |
| 51 | +} as const |
| 52 | +type BuilderIdStage = (typeof stages)[keyof typeof stages] |
| 53 | +
|
| 54 | +export default defineComponent({ |
| 55 | + name: 'CredentialsForm', |
| 56 | + extends: BaseAuthForm, |
| 57 | + components: { FormTitle }, |
| 58 | + props: { |
| 59 | + state: { |
| 60 | + type: Object as PropType<BaseBuilderIdState>, |
| 61 | + required: true, |
| 62 | + }, |
| 63 | + stages: { |
| 64 | + type: Object as PropType<typeof stages>, |
| 65 | + default: stages, |
| 66 | + }, |
| 67 | + }, |
| 68 | + data() { |
| 69 | + return { |
| 70 | + stage: stages.START as BuilderIdStage, |
| 71 | + isConnected: false, |
| 72 | + builderIdCode: '', |
| 73 | + canShowAll: false, |
| 74 | + } |
| 75 | + }, |
| 76 | + async created() { |
| 77 | + await this.update() |
| 78 | + this.canShowAll = true |
| 79 | + }, |
| 80 | + methods: { |
| 81 | + async startSignIn() { |
| 82 | + this.stage = this.stages.WAITING_ON_USER |
| 83 | + await this.state.startBuilderIdSetup() |
| 84 | + await this.update() |
| 85 | + }, |
| 86 | + async update() { |
| 87 | + this.stage = await this.state.stage() |
| 88 | + this.isConnected = await this.state.isAuthConnected() |
| 89 | + this.emitAuthConnectionUpdated(this.state.id) |
| 90 | + }, |
| 91 | + async signout() { |
| 92 | + await this.state.signout() |
| 93 | +
|
| 94 | + this.update() |
| 95 | + }, |
| 96 | + }, |
| 97 | +}) |
| 98 | +
|
| 99 | +/** |
| 100 | + * Manages the state of Builder ID. |
| 101 | + */ |
| 102 | +abstract class BaseBuilderIdState implements AuthStatus { |
| 103 | + protected _stage: BuilderIdStage = stages.START |
| 104 | +
|
| 105 | + abstract get id(): AuthFormId |
| 106 | + protected abstract _startBuilderIdSetup(): Promise<void> |
| 107 | + abstract isAuthConnected(): Promise<boolean> |
| 108 | +
|
| 109 | + async startBuilderIdSetup(): Promise<void> { |
| 110 | + this._stage = stages.WAITING_ON_USER |
| 111 | + return this._startBuilderIdSetup() |
| 112 | + } |
| 113 | +
|
| 114 | + async stage(): Promise<BuilderIdStage> { |
| 115 | + const isAuthConnected = await this.isAuthConnected() |
| 116 | + this._stage = isAuthConnected ? stages.CONNECTED : stages.START |
| 117 | + return this._stage |
| 118 | + } |
| 119 | +
|
| 120 | + async signout(): Promise<void> { |
| 121 | + await client.signoutBuilderId() |
| 122 | + } |
| 123 | +} |
| 124 | +
|
| 125 | +export class CodeWhispererBuilderIdState extends BaseBuilderIdState { |
| 126 | + override get id(): AuthFormId { |
| 127 | + return authForms.BUILDER_ID_CODE_WHISPERER |
| 128 | + } |
| 129 | +
|
| 130 | + override isAuthConnected(): Promise<boolean> { |
| 131 | + return client.isCodeWhispererBuilderIdConnected() |
| 132 | + } |
| 133 | +
|
| 134 | + protected override _startBuilderIdSetup(): Promise<void> { |
| 135 | + return client.startCodeWhispererBuilderIdSetup() |
| 136 | + } |
| 137 | +} |
| 138 | +
|
| 139 | +export class CodeCatalystBuilderIdState extends BaseBuilderIdState { |
| 140 | + override get id(): AuthFormId { |
| 141 | + return authForms.BUILDER_ID_CODE_CATALYST |
| 142 | + } |
| 143 | +
|
| 144 | + override isAuthConnected(): Promise<boolean> { |
| 145 | + return client.isCodeCatalystBuilderIdConnected() |
| 146 | + } |
| 147 | +
|
| 148 | + protected override _startBuilderIdSetup(): Promise<void> { |
| 149 | + return client.startCodeCatalystBuilderIdSetup() |
| 150 | + } |
| 151 | +} |
| 152 | +</script> |
| 153 | +<style> |
| 154 | +@import './sharedAuthForms.css'; |
| 155 | +@import '../shared.css'; |
| 156 | +
|
| 157 | +#builder-id-form { |
| 158 | + width: 250px; |
| 159 | + height: fit-content; |
| 160 | +} |
| 161 | +</style> |
0 commit comments