Skip to content

Instantly share code, notes, and snippets.

View alexanmtz's full-sized avatar
🎯
Focusing

Alexandre Magno alexanmtz

🎯
Focusing
View GitHub Profile
@alexanmtz
alexanmtz / auth.js
Last active April 5, 2021 15:32
Routes on front-end using react router
class Auth {
/**
* Authenticate a user. Save a token string in Local Storage
*
* @param {string} token
*/
/* eslint-disable no-undef */
static authenticateUser (token) {
localStorage.setItem('token', token)
}
@alexanmtz
alexanmtz / session.js
Created January 13, 2019 20:52
A Session component implemented on React to store in localStorage the user token
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import Auth from '../../modules/auth'
class Session extends Component {
componentWillMount () {
const token = this.props.match.params.token
const referer = Auth.getReferer()
Auth.authenticateUser(token)
@alexanmtz
alexanmtz / auth.js
Created January 13, 2019 20:50
Routes to authenticate
const express = require('express')
const router = express.Router()
const passport = require('passport')
const authenticationHelpers = require('../../authenticationHelpers')
require('../../../loading/loading')
const secure = require('./secure')
router.get('/authenticated', authenticationHelpers.isAuth)
router.get('/authorize/github', passport.authenticate('github', { scope: ['user:email'], accessType: 'offline' }))
/**
* Authentication helpers to determine if a user is logged in or not
* before a route returns information to the response
*/
const userExist = require('../modules/users').userExists
const jwt = require('jsonwebtoken')
function isAuthOrRedirect (req, res, next) {
if (req.isAuthenticated()) return next()
@alexanmtz
alexanmtz / passport.js
Created January 13, 2019 20:42
Authentication with Node.js, Express, Sequelize, JWT and webtokens
const {
github
} = require('./secrets')
const passport = require('passport')
const gitHubStrategy = require('passport-github2').Strategy
const LocalStrategy = require('passport-local').Strategy
const passportJWT = require('passport-jwt')
const ExtractJWT = passportJWT.ExtractJwt
const JWTStrategy = passportJWT.Strategy
const jwt = require('jsonwebtoken')
@alexanmtz
alexanmtz / git-export
Created January 2, 2019 10:01 — forked from kristofferh/git-export
"Export" a git repository to zip file
git archive --format zip --output /full/path/to/zipfile.zip master
@alexanmtz
alexanmtz / server.js
Created December 4, 2018 19:11
A server that will receive a route sensor with credentials to notify by e-mail the user
const express = require('express')
const app = express()
const cors = require('cors')
const bodyParser = require('body-parser')
const Notify = require('./mail')
if (process.env.NODE_ENV !== 'production') {
require('dotenv').config()
app.use(cors())
}
@alexanmtz
alexanmtz / device.py
Created December 4, 2018 19:04
A NodeMCU Micropython script that connects on Wifi and send values about humidity
import network
from machine import ADC
from machine import Timer
import urequests
import os
secret = os.environ['SECRET']
def send_sensor_values():
adc = ADC(0)
@alexanmtz
alexanmtz / chips.jsx
Created October 22, 2018 19:27
Improve a react JSX code
<div className={ classes.chipContainer }>
<Chip
label=' daqui uma semana '
className={ classes.chip }
onClick={ () => this.pickTaskDeadline(7) }
/>
<Chip
label=' daqui quinze dias '
className={ classes.chip }
onClick={ () => this.pickTaskDeadline(15) }
@alexanmtz
alexanmtz / checkout.component.test.js
Created September 14, 2018 20:49
Jest component testing using Enzyme with React
import React from 'react'
import { CheckoutFormPure } from '../../src/components/checkout/checkout-form'
import { mount, configure } from 'enzyme'
import Adapter from 'enzyme-adapter-react-15'
configure({ adapter: new Adapter() })
describe('components', () => {
describe('checkout component', () => {
it('should start a new checkout form with empty state', () => {