-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: device identification on analytics
- Loading branch information
1 parent
d135030
commit 9563498
Showing
13 changed files
with
370 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { PhoneDirective } from './phone.directive'; | ||
export { Recaptcha } from './recaptcha/re-captcha.directive'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { Directive, input, OnDestroy, OnInit, output } from '@angular/core'; | ||
import { AsyncSubject, lastValueFrom, switchMap } from 'rxjs'; | ||
|
||
declare global { | ||
interface Window { | ||
grecaptcha: { | ||
ready: (fn: () => void) => void; | ||
execute: (key: string, opts: { action: string }) => Promise<string>; | ||
}, | ||
recaptchaLoaded?: () => void; | ||
} | ||
} | ||
|
||
@Directive({ | ||
selector: '[tmReCaptcha]' | ||
}) | ||
export class Recaptcha implements OnInit, OnDestroy { | ||
private static scriptElement?: HTMLScriptElement; | ||
private static instanceCount = 0; | ||
private static loaded = new AsyncSubject<void>(); | ||
readonly recaptchaKey = input.required<string>(); | ||
readonly ready = output(); | ||
|
||
constructor() { | ||
Recaptcha.instanceCount++; | ||
} | ||
|
||
async getToken(action: string) { | ||
if (Recaptcha.loaded.closed) | ||
return await window.grecaptcha.execute(this.recaptchaKey(), { action }); | ||
return await lastValueFrom(Recaptcha.loaded.pipe(switchMap(() => window.grecaptcha.execute(this.recaptchaKey(), { action })))); | ||
} | ||
|
||
ngOnInit(): void { | ||
let script = Recaptcha.scriptElement; | ||
if (!script) { | ||
window.recaptchaLoaded = () => { | ||
Recaptcha.loaded.next(); | ||
Recaptcha.loaded.complete(); | ||
this.ready.emit(); | ||
} | ||
script = document.createElement('script'); | ||
Recaptcha.scriptElement = script; | ||
const url = new URL('/recaptcha/api.js', 'https://google.com'); | ||
url.searchParams.set('render', this.recaptchaKey()); | ||
url.searchParams.set('onload', 'recaptchaLoaded'); | ||
url.searchParams.set('trustedtypes', 'true'); | ||
|
||
script.src = url.toString(); | ||
script.async = true; | ||
script.defer = true; | ||
script.id = 'recaptcha-script'; | ||
|
||
document.head.appendChild(script); | ||
} else { | ||
this.ready.emit(); | ||
} | ||
} | ||
|
||
ngOnDestroy() { | ||
Recaptcha.instanceCount = Math.max(Recaptcha.instanceCount - 1, 0); | ||
const instanceCount = Recaptcha.instanceCount; | ||
if (instanceCount > 0) return; | ||
document.getElementById('recaptcha-script')?.remove(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<div tmReCaptcha [recaptchaKey]="siteKey" class="flex items-center justify-center h-full" (ready)="onRecaptchaReady()"> | ||
<p> | ||
@if(failed()) { | ||
Broken URL. Redirecting to home page... | ||
}@else { | ||
Redirecting... | ||
} | ||
</p> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
:host { | ||
@apply w-full h-full block | ||
} |
Oops, something went wrong.