Skip to content

Instantly share code, notes, and snippets.

@shalvah
shalvah / google-onetime-oauth-cli.js
Created March 15, 2020 19:42
How to get a one-time access token from Google
const TOKEN_PATH = 'token.json';
const SCOPES = ['https://www.googleapis.com/auth/spreadsheets'];
const gapi = require('googleapis');
const clientSecret = process.env.GOOGLE_CLIENT_SECRET;
const clientId = process.env.GOOGLE_CLIENT_ID;
const redirectUri = process.env.GOOGLE_REDIRECT_URI;
const oAuth2Client = new gapi.google.auth.OAuth2(clientId, clientSecret, redirectUri);
const fs = require('fs').promises;
2020-02-05 14:08:14.431 [apm-server-healthcheck] DEBUG co.elastic.apm.agent.report.ApmServerHealthChecker - Starting healthcheck to http://172.24.102.69:8200/
2020-02-05 14:08:14.442 [main] DEBUG co.elastic.apm.agent.impl.payload.SystemInfo - Could not parse container ID from '/proc/self/cgroup' line: 10:memory:/
2020-02-05 14:08:14.442 [main] DEBUG co.elastic.apm.agent.impl.payload.SystemInfo - Could not parse container ID from '/proc/self/cgroup' line: 9:hugetlb:/
2020-02-05 14:08:14.442 [main] DEBUG co.elastic.apm.agent.impl.payload.SystemInfo - Could not parse container ID from '/proc/self/cgroup' line: 8:devices:/
2020-02-05 14:08:14.443 [main] DEBUG co.elastic.apm.agent.impl.payload.SystemInfo - Could not parse container ID from '/proc/self/cgroup' line: 7:blkio:/
2020-02-05 14:08:14.443 [main] DEBUG co.elastic.apm.agent.impl.payload.SystemInfo - Could not parse container ID from '/proc/self/cgroup' line: 6:cpuacct,cpu:/
2020-02-05 14:08:14.443 [main] DEBUG co.elastic.apm.agent.impl.payload.SystemInfo -
2020-02-05 11:20:19.512 [apm-server-healthcheck] DEBUG co.elastic.apm.agent.report.ApmServerHealthChecker - Starting healthcheck to http://172.24.102.69:8200/
2020-02-05 11:20:19.522 [main] DEBUG co.elastic.apm.agent.impl.payload.SystemInfo - Could not parse container ID from '/proc/self/cgroup' line: 10:net_cls:/
2020-02-05 11:20:19.522 [main] DEBUG co.elastic.apm.agent.impl.payload.SystemInfo - Could not parse container ID from '/proc/self/cgroup' line: 9:memory:/
2020-02-05 11:20:19.523 [main] DEBUG co.elastic.apm.agent.impl.payload.SystemInfo - Could not parse container ID from '/proc/self/cgroup' line: 8:hugetlb:/
2020-02-05 11:20:19.523 [main] DEBUG co.elastic.apm.agent.impl.payload.SystemInfo - Could not parse container ID from '/proc/self/cgroup' line: 7:perf_event:/
2020-02-05 11:20:19.523 [main] DEBUG co.elastic.apm.agent.impl.payload.SystemInfo - Could not parse container ID from '/proc/self/cgroup' line: 6:devices:/
2020-02-05 11:20:19.524 [main] DEBUG co.elastic.apm.agent.impl.payload.SystemInfo
module.exports = {
"env": {
"commonjs": true,
"es2021": true
},
"extends": "eslint:recommended",
"parserOptions": {
"ecmaVersion": 13
},
"rules": {
// without event sourcing
function transferMoneyBetweenAccounts(amount, fromAccount, toAccount) {
BankAccount.where({ id: fromAccount.id })
.decrement({ amount });
BankAccount.where({ id: toAccount.id })
.increment({ amount });
}
function makeOnlinePayment(account, amount) {
BankAccount.where({ id: account.id })
// the base command class or interface
class Command {
handle() {
}
}
class CreateUserCommand extends Command {
constructor(user) {
super();
this.user = user;
@shalvah
shalvah / twitter-redux-example.js
Last active December 30, 2018 11:35
Basic example of how Redux could be used to manage state in a frontend app like Twitter Lite (mobile.twitter.com)
import { createStore } from 'redux';
// our reducer
const tweets = (state = {tweets: []}, action) => {
switch (action.type) {
// we'll handle only one action: when new tweets arrive
case 'SHOW_NEW_TWEETS':
state.numberOfNewTweets = action.count;
return state.tweets.concat([action.tweets]);
default:
@shalvah
shalvah / debounce.js
Last active December 22, 2018 09:55
Implementation of debounce functionality in JavaScript
function debounce(f, t) {
return function (args) {
let previousCall = this.lastCall;
this.lastCall = Date.now();
if (previousCall && ((this.lastCall - previousCall) <= t)) {
clearTimeout(this.lastCallTimer);
}
this.lastCallTimer = setTimeout(() => f(args), t);
}
}
@shalvah
shalvah / throttle.js
Last active October 24, 2019 09:03
Implementation of throttle functionality in JavaScript
function throttle(f, t) {
return function (args) {
let previousCall = this.lastCall;
this.lastCall = Date.now();
if (previousCall === undefined // function is being called for the first time
|| (this.lastCall - previousCall) > t) { // throttle time has elapsed
f(args);
}
}
}

Why use this?

Supposing you're building an app (for instance, a voting app), and you want to restrict it to UNN students. Simply ask them to login with their UNN Portal details, and pass them to this API to verify their identity. (Please do not store those credentials 🙏).

How to Use

There's only one endpoint: https://unn-api.herokuapp.com/students/auth

Make a POST request with the following parameters (form-data or application/json content types acceptable)

  • username: The student s unnportal.unn.edu.ng username. Example: 2013/123456
  • password: The student s unnportal.unn.edu.ng password