Skip to content

Instantly share code, notes, and snippets.

@maoosi
Last active October 26, 2021 18:24

Revisions

  1. maoosi revised this gist Jan 26, 2021. No changes.
  2. maoosi revised this gist Jan 26, 2021. No changes.
  3. maoosi revised this gist Jan 26, 2021. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions amplifyauth-scheme.ts
    Original file line number Diff line number Diff line change
    @@ -39,10 +39,10 @@ export default class AmplifyAuthScheme {
    async fetchUser() {
    try {
    const user = await this._Auth().currentAuthenticatedUser()
    const userAttributes = process.server
    const { attributes } = process.server
    ? await this._Auth().currentUserInfo()
    : user
    this.$auth.setUser(userAttributes)
    this.$auth.setUser(attributes)
    return user
    } catch (error) {
    this.$auth.callOnError(error, { method: 'fetchUser' })
  4. maoosi revised this gist Jan 26, 2021. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions amplifyauth-scheme.ts
    Original file line number Diff line number Diff line change
    @@ -38,8 +38,10 @@ export default class AmplifyAuthScheme {

    async fetchUser() {
    try {
    let userAttributes = await this._Auth().currentAuthenticatedUser()
    if (process.server) userAttributes = await this._Auth().currentUserInfo()
    const user = await this._Auth().currentAuthenticatedUser()
    const userAttributes = process.server
    ? await this._Auth().currentUserInfo()
    : user
    this.$auth.setUser(userAttributes)
    return user
    } catch (error) {
  5. maoosi revised this gist Jan 26, 2021. 1 changed file with 6 additions and 25 deletions.
    31 changes: 6 additions & 25 deletions amplifyauth-scheme.ts
    Original file line number Diff line number Diff line change
    @@ -21,20 +21,12 @@ export default class AmplifyAuthScheme {
    }

    async mounted() {
    Amplify.configure({
    /* aws config here */
    /* https://docs.amplify.aws/lib/auth/getting-started/q/platform/js#configure-your-application */
    })
    // https://docs.amplify.aws/lib/auth/getting-started/q/platform/js#configure-your-application
    Amplify.configure({ /* aws config here */ })
    return this.$auth.fetchUserOnce()
    }

    async login({
    username,
    password,
    }: {
    username: string
    password: string
    }) {
    async login({ username, password }:{ username: string, password: string }) {
    try {
    await this.$auth.reset()
    await this._Auth().signIn(username, password)
    @@ -46,20 +38,9 @@ export default class AmplifyAuthScheme {

    async fetchUser() {
    try {
    const user = await this._Auth().currentAuthenticatedUser()

    if (process.server) {
    const infos = await this._Auth().currentUserInfo()

    this.$auth.setUser({
    email: infos.attributes.email,
    })
    } else {
    this.$auth.setUser({
    email: user.attributes.email,
    })
    }

    let userAttributes = await this._Auth().currentAuthenticatedUser()
    if (process.server) userAttributes = await this._Auth().currentUserInfo()
    this.$auth.setUser(userAttributes)
    return user
    } catch (error) {
    this.$auth.callOnError(error, { method: 'fetchUser' })
  6. maoosi revised this gist Jan 26, 2021. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions amplifyauth-scheme.ts
    Original file line number Diff line number Diff line change
    @@ -23,6 +23,7 @@ export default class AmplifyAuthScheme {
    async mounted() {
    Amplify.configure({
    /* aws config here */
    /* https://docs.amplify.aws/lib/auth/getting-started/q/platform/js#configure-your-application */
    })
    return this.$auth.fetchUserOnce()
    }
  7. maoosi revised this gist Jan 26, 2021. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion amplifyauth-scheme.ts
    Original file line number Diff line number Diff line change
    @@ -21,7 +21,9 @@ export default class AmplifyAuthScheme {
    }

    async mounted() {
    Amplify.configure(amplifyConfig)
    Amplify.configure({
    /* aws config here */
    })
    return this.$auth.fetchUserOnce()
    }

  8. maoosi renamed this gist Jan 26, 2021. 1 changed file with 1 addition and 11 deletions.
    12 changes: 1 addition & 11 deletions amplifyAuthScheme.ts → amplifyauth-scheme.ts
    Original file line number Diff line number Diff line change
    @@ -21,17 +21,7 @@ export default class AmplifyAuthScheme {
    }

    async mounted() {
    if (process.server) {
    const amplifyConfig = await this.$auth.ctx.store.dispatch(
    'getAmplifyConfig',
    {
    runtimeConfig: this.$auth.ctx.$config,
    isDev: this.$auth.ctx.isDev,
    }
    )

    Amplify.configure(amplifyConfig)
    }
    Amplify.configure(amplifyConfig)
    return this.$auth.fetchUserOnce()
    }

  9. maoosi revised this gist Jan 26, 2021. No changes.
  10. maoosi created this gist Jan 26, 2021.
    88 changes: 88 additions & 0 deletions amplifyAuthScheme.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,88 @@
    import { Amplify, Auth, withSSRContext } from 'aws-amplify'
    import { Auth as NuxtAuth } from '@nuxtjs/auth-next'

    export interface AmplifyAuthSchemeOptions {
    name: string
    }

    export default class AmplifyAuthScheme {
    public $auth: NuxtAuth
    public options: AmplifyAuthSchemeOptions

    constructor($auth: NuxtAuth, options: AmplifyAuthSchemeOptions) {
    this.$auth = $auth
    this.options = options
    }

    _Auth(): typeof Auth {
    return process.server
    ? withSSRContext({ req: this.$auth.ctx.req }).Auth
    : Auth
    }

    async mounted() {
    if (process.server) {
    const amplifyConfig = await this.$auth.ctx.store.dispatch(
    'getAmplifyConfig',
    {
    runtimeConfig: this.$auth.ctx.$config,
    isDev: this.$auth.ctx.isDev,
    }
    )

    Amplify.configure(amplifyConfig)
    }
    return this.$auth.fetchUserOnce()
    }

    async login({
    username,
    password,
    }: {
    username: string
    password: string
    }) {
    try {
    await this.$auth.reset()
    await this._Auth().signIn(username, password)
    return this.$auth.fetchUser()
    } catch (error) {
    this.$auth.callOnError(error, { method: 'login' })
    }
    }

    async fetchUser() {
    try {
    const user = await this._Auth().currentAuthenticatedUser()

    if (process.server) {
    const infos = await this._Auth().currentUserInfo()

    this.$auth.setUser({
    email: infos.attributes.email,
    })
    } else {
    this.$auth.setUser({
    email: user.attributes.email,
    })
    }

    return user
    } catch (error) {
    this.$auth.callOnError(error, { method: 'fetchUser' })
    }
    }

    reset() {
    this.$auth.setUser(false)
    }

    async logout() {
    try {
    await this._Auth().signOut()
    return this.$auth.reset()
    } catch (error) {
    this.$auth.callOnError(error, { method: 'logout' })
    }
    }
    }