Skip to content

Instantly share code, notes, and snippets.

View fl0wo's full-sized avatar
🎯
Focusing

Florian Sabani fl0wo

🎯
Focusing
View GitHub Profile
@fl0wo
fl0wo / stytchClient.ts
Last active December 12, 2022 22:16
File used to instantiate stytchUIClient
import {StytchUIClient} from "@stytch/vanilla-js";
export const stytchUIClient = new StytchUIClient(
'<here the stytch public token>'
);
@fl0wo
fl0wo / stytch-join.component.ts
Last active December 12, 2022 20:30
Angular html content that will be replaced by Stytch UI SDK
import {stytchUIClient} from "../../environments/stytchClient";
@ViewChild('magic_stytch_login')
divLogin:any;
// ...
stytchUIClient.mountLogin({
elementId: '#' + this.divLogin.nativeElement.id,
config: config,
@fl0wo
fl0wo / stytch-join.component.ts
Created December 12, 2022 17:31
Stytch component js used to inject UI on div
import {AfterViewInit, Component, OnInit, ViewChild} from '@angular/core';
import {environment} from "../../environments/environment";
import {stytchUIClient} from "../../environments/stytchClient";
import {OAuthProviders, Products, StytchUIClient} from "@stytch/vanilla-js";
import {OneTapPositions, StyleConfig, StytchLoginConfig} from "@stytch/core/public";
@Component({
selector: 'app-stytch-join',
templateUrl: './stytch-join.component.html',
styleUrls: ['./stytch-join.component.scss']
@fl0wo
fl0wo / stytch-join.component.ts
Created December 12, 2022 17:40
StytchLoginConfig
const config:StytchLoginConfig = {
products: [
Products.oauth
],
oauthOptions:{
providers:[
{
type: OAuthProviders.Google,
one_tap:true,
position:OneTapPositions.embedded
@fl0wo
fl0wo / aws-api-gtw-definition.ts
Last active December 12, 2022 20:12
Generation of RestApi object in AWS CDK.
export const addUserApiGateway = (scope: Construct, options: InfrastructureOptions) => {
const api = new RestApi(scope, "ApiGatewayTutorial", {
deployOptions: {
cacheTtl: Duration.seconds(0),
throttlingBurstLimit: 1, // can ask JWT max 1 time!
throttlingRateLimit: 1,
},
defaultCorsPreflightOptions: {
@fl0wo
fl0wo / check_stytch_authorizer.ts
Last active December 12, 2022 20:42
Integration of check_stytch_authorizer.ts
import {authenticateJwtLocal} from "../loadStytch";
export const handler = async (event:any) => {
const token = event.authorizationToken;
const isAuthenticated = await authenticateJwtLocal(token);
return canContinue(isAuthenticated?'Allow':'Deny');
};
function canContinue(effect: string) {
return {
@fl0wo
fl0wo / register.ts
Last active December 12, 2022 21:08
Integration of register.ts
export const handler = async (event:any) => {
// Get token from Stytch
const token = event.queryStringParameters.token;
// Get instance of client
const client = loadStytch();
// Grant jwt
const response:AuthenticateResponse = await client.oauth.authenticate(token,{
session_duration_minutes: 60
@fl0wo
fl0wo / ssm-utils.ts
Last active December 12, 2022 20:56
Cool AWS SSM Functions utils for PUT/GET/DELETE keys
import {SSM} from "aws-sdk";
export async function getParameterValue(parameterName: string) {
const param = await new SSM()
.getParameter({Name: parameterName,WithDecryption: true})
.promise();
const value = param.Parameter?.Value as string;
if (!value) throw new Error(`Can not find SSM parameter with name ${parameterName}`);
return value;
}
@fl0wo
fl0wo / token.ts
Last active December 12, 2022 20:51
Integration of token.ts
export const handler: any = async (event:any) => {
try {
const code = event.queryStringParameters!.code;
const jwtKey = await getParameterValue(
wrapCodeWithPrefix(code)
);
const expiresIn = whenJwtExpiresLocal(jwtKey);
if (Number.isInteger(expiresIn)) {
@fl0wo
fl0wo / login.ts
Last active December 12, 2022 21:14
Integration of login stytch lambda
export const handler = async (event:any) => {
// Ge the token from Stytch
const token = event.queryStringParameters.token;
const client = loadStytch();
// Grant JWT
const response:AuthenticateResponse = await client.oauth.authenticate(token,{
session_duration_minutes: 60
});
const userId = response.user_id;
const sessionId = getSafeOrThrow(response.session?.session_id,'Session id cannot be null');