Skip to content

Instantly share code, notes, and snippets.

View JosephScript's full-sized avatar
💭
🚀 Changed my handle to @JosephScript!

Joseph A. Szczesniak JosephScript

💭
🚀 Changed my handle to @JosephScript!
View GitHub Profile
@JosephScript
JosephScript / giantBomb.js
Created November 16, 2015 19:58
A simple Giantbomb API request using JSONP for cross origin support.
$.ajax ({
type: 'GET',
dataType: 'jsonp',
crossDomain: true,
jsonp: 'json_callback',
url: 'http://www.giantbomb.com/api/search/?format=jsonp&api_key=[YOUR_API_KEY]&query=batman'
}).done(function(data) {
alert("success:", data);
}).fail(function() {
alert("error");
@JosephScript
JosephScript / catchAll.js
Last active January 29, 2016 18:40
"Catchall" router for angular apps. Include this router LAST.
var express = require('express');
var router = express.Router();
/* handle root angular route redirects */
router.get('/*', function(req, res, next){
var url = req.originalUrl;
if (url.split('.').length > 1){
next();
} else {
// handles angular urls. i.e. anything without a '.' in the url (so static files aren't handled)
@JosephScript
JosephScript / LoginCtrl.js
Created September 22, 2015 14:33
A login controller for AngularJS. This relies on authService.js for token storage, and retrieval.
app.controller('LoginCtrl', ['$scope', '$http', '$location', 'authService',
function ($scope, $http, $location, authService) {
$scope.submit = function () {
$http.post('/authenticate', this.form)
.success(function (data, status, headers, config) {
// save json web token in session storage
authService.saveToken(data.token);
@JosephScript
JosephScript / authInterceptor.js
Last active March 14, 2017 05:24
An authorization interceptor for AngularJS. This relies on an authorization service, and will send JWT headers with every request. If a 401 error is encountered, the user is redirected to the login page.
app.factory('authInterceptor', ['$q', '$location', 'authService', function ($q, $location, authService) {
return {
request: function (config) {
config.headers = config.headers || {};
if (authService.isAuthed()) {
config.headers.Authorization = 'Bearer ' + authService.getToken();
}
return config;
},
response: function (response) {
@JosephScript
JosephScript / authService.js
Created September 22, 2015 14:25
This is an AngularJS authentication service for using JSON Web Tokens
app.service('authService', ['$window', function ($window) {
this.parseJwt = function (token) {
if (token) {
var base64Url = token.split('.')[1];
var base64 = base64Url.replace('-', '+').replace('_', '/');
return JSON.parse($window.atob(base64));
} else return {};
};