Skip to content

Commit

Permalink
chore: use W3mFrameHelpers to get localstorage stored las login attem…
Browse files Browse the repository at this point in the history
…pt time
  • Loading branch information
tomiir committed Jan 2, 2024
1 parent b4faba2 commit 9170833
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
} from '@web3modal/core'
import { state } from 'lit/decorators.js'
import styles from './styles.js'
import { W3mFrameHelpers } from '@web3modal/wallet'

// -- Helpers ------------------------------------------- //
const OTP_LENGTH = 6
Expand All @@ -26,7 +27,7 @@ export class W3mEmailVerifyOtpView extends LitElement {
// -- State & Properties -------------------------------- //
@state() private loading = false

@state() private timeoutTimeLeft = 30
@state() private timeoutTimeLeft = W3mFrameHelpers.getTimeToNextEmailLogin()

private OTPTimeout: NodeJS.Timeout | undefined

Expand Down Expand Up @@ -80,10 +81,9 @@ export class W3mEmailVerifyOtpView extends LitElement {

// -- Private ------------------------------------------- //
private startOTPTimeout() {
this.timeoutTimeLeft = 30
this.OTPTimeout = setInterval(() => {
if (this.timeoutTimeLeft > 0) {
this.timeoutTimeLeft -= 1
this.timeoutTimeLeft = W3mFrameHelpers.getTimeToNextEmailLogin()
} else {
clearInterval(this.OTPTimeout)
}
Expand Down
12 changes: 12 additions & 0 deletions packages/wallet/src/W3mFrameHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,17 @@ export const W3mFrameHelpers = {
throw new Error(`Please try again after ${cooldownSec} seconds`)
}
}
},

getTimeToNextEmailLogin() {
const lastEmailLoginTime = W3mFrameStorage.get(W3mFrameConstants.LAST_EMAIL_LOGIN_TIME)
if (lastEmailLoginTime) {
const difference = Date.now() - Number(lastEmailLoginTime)
if (difference < 30_000) {
return Math.ceil((30_000 - difference) / 1000)
}
}

return 0
}
}

0 comments on commit 9170833

Please sign in to comment.