Skip to content

Instantly share code, notes, and snippets.

View kennethlng's full-sized avatar

Kenneth Ng kennethlng

View GitHub Profile
// AuthenticationManager.cs
public Task SignInAnonymously()
{
return auth.SignInAnonymouslyAsync().ContinueWith(task =>
{
if (task.IsCanceled)
{
Debug.LogError("SignInAnonymouslyAsync was canceled.");
OnSignInFailed?.Invoke();
import React from 'react';
const check = (action, permission) => {
if (action === permission) return true;
return false;
}
const Authorization = props => {
const { action, permission } = this.props;
@kennethlng
kennethlng / FirebaseFirestoreStore.swift
Created April 20, 2020 20:52
Firebase Firestore store
import FirebaseFirestore
class FirestoreStore<T: FirestoreModel> {
private var listener: ListenerRegistration?
func listenDocument(documentID: String, completion: @escaping (Result<T, Error>) -> Void) {
unlisten()
listener = T.collectionRef.document(documentID).addSnapshotListener { (documentSnapshot, error) in
guard let document = documentSnapshot else {
@kennethlng
kennethlng / FirebaseAuthenticationStore.swift
Last active January 12, 2024 09:39
Firebase authentication store
import Foundation
import FirebaseAuth
class AuthenticationStore: ObservableObject {
var handle: AuthStateDidChangeListenerHandle?
func listen(completion: @escaping (FirebaseAuth.User?) -> Void) {
handle = Auth.auth().addStateDidChangeListener { (auth, authUser) in
completion(authUser)
}
const ProtectedRoute = ({ component: Component, ...rest }) => (
<Route {...rest} render={(props) => (
auth.isAuthenticated ?
<Component {...props} /> : <Redirect to={{ pathname: '/login', state: { from: props.location }}} />
)} />
)
@kennethlng
kennethlng / Authorization.js
Last active March 25, 2020 19:28
React Authorization Component
<Authorization
role={user.role}
perform="write:product"
isAuthorized={() => <AddProductPage />}
isNotAuthorized={() => <h2>Access denied. Please speak with your administrator.</h2>}
/>
// State.cs
public class State : ScriptableObject
{
public Transition transition;
private bool canChangeState;
// Called each frame
public void UpdateState (StateMachine _stateMachine)
{
// State.cs
using UnityEngine.Events;
[CreateAssetMenu]
public class State : ScriptableObject
{
public UnityEvent enterStateEvent;
public UnityEvent exitStateEvent;