Skip to content

Instantly share code, notes, and snippets.

View Mulperi's full-sized avatar
🎯
Focusing

Mika Mulperi Lakanen Mulperi

🎯
Focusing
View GitHub Profile
@Mulperi
Mulperi / auth-guard.service.ts
Created January 12, 2019 10:25
Simple auth guard with cognito session
@Injectable({
providedIn: 'root'
})
export class AuthGuardService implements CanActivate {
constructor(public cognitoService: CognitoService, public router: Router) {}
canActivate(): Observable<boolean> {
return this.cognitoService
.getSession()
.pipe(map(session => (session.isValid() ? true : false)));
@Mulperi
Mulperi / post.service.ts
Created January 12, 2019 20:09
Http post with authorization header
public savePost(post: string): Observable<any> {
return this.cognitoService.getAccessToken().pipe(
concatMap(accessToken => {
return this.http.post(
'http://localhost:3000/posts',
{ post },
{
headers: {
Authorization: accessToken.jwtToken
}
@Mulperi
Mulperi / id-attribute.html
Created January 16, 2019 07:45
Dynamic id-attribute in Angular *ngFor-loop
<div *ngFor="let item of [1,2,3]; let i = index" [attr.id]="'item-'+ i">
{{ item }}
</div>
@Mulperi
Mulperi / cognito.service.ts
Last active January 25, 2019 11:08
Cognito service in Angular using AWS Amplify
import { Injectable } from '@angular/core';
import { from, Observable, throwError } from 'rxjs';
import { Auth } from 'aws-amplify';
import { catchError, map } from 'rxjs/operators';
@Injectable({
providedIn: 'root'
})
export class CognitoService {
@Mulperi
Mulperi / template.yaml
Last active January 26, 2019 05:57
Minimal Cloudformation template for Cognito (and app client) and DynamoDB
AWSTemplateFormatVersion: '2010-09-09'
Description: 'mulperiCMS AWS infra'
Parameters:
StackName:
Type: String
Resources:
UserPool:
Type: AWS::Cognito::UserPool
@Mulperi
Mulperi / cognito.service.ts
Last active January 26, 2019 07:34
Cognito service in Angular using amazon-cognito-identity-js (in progress)
import { Injectable } from '@angular/core';
import {
CognitoUserPool,
AuthenticationDetails,
CognitoUser,
CognitoUserSession,
CognitoUserAttribute,
CognitoAccessToken,
CognitoIdToken
} from 'amazon-cognito-identity-js';
@Mulperi
Mulperi / truncate.pipe.ts
Created January 30, 2019 12:56
Simple truncate pipe in Angular
import { Pipe, PipeTransform } from '@angular/core';
/*
A pipe is a class decorated with pipe metadata.
The pipe class implements the PipeTransform interface's transform method that accepts an input value followed by optional parameters and returns the transformed value.
There will be one additional argument to the transform method for each parameter passed to the pipe. Your pipe has one such parameter: the exponent.
To tell Angular that this is a pipe, you apply the @Pipe decorator, which you import from the core Angular library.
The @Pipe decorator allows you to define the pipe name that you'll use within template expressions. It must be a valid JavaScript identifier.
@Mulperi
Mulperi / tooltip.directive.ts
Created February 5, 2019 13:15
Angular tooltip directive example
import { Directive, Input, ElementRef, OnInit } from '@angular/core';
@Directive({
selector: '[mulperiTooltip]',
})
export class TooltipDirective implements OnInit {
@Input() mulperiDescription: string;
constructor(private el: ElementRef) {}
@Mulperi
Mulperi / item.reducer.ts
Created March 8, 2019 12:30
How to update entities object
/*
Will look like this:
{
1: { <- Parent item id
1: { <- Child item id and item itself
childId: "abc"
}
}
}
*/
@Mulperi
Mulperi / component.ts
Created March 18, 2019 13:01
JSON Array to CSV
import { saveAs } from "file-saver";
import * as converter from "json-2-csv";
converter.json2csv(this.items, (err, csv) => {
if (!err) {
const blob = new Blob([csv], { type: "text/csv;charset=utf-8;" });
saveAs(blob, "export.csv");
} else {
console.log("Error converting data to CSV.");
}