Skip to content

Instantly share code, notes, and snippets.

View NicholusMuwonge's full-sized avatar
💭
Job Hunting

nicks-bro NicholusMuwonge

💭
Job Hunting
View GitHub Profile
import React from "react";
import { GoogleAPI, GoogleLogin, GoogleLogout } from "react-google-oauth";
import FacebookLogin from 'react-facebook-login';
function App() {
return (
<div className="App">
<div className="App-header">
namespace :api, defaults: { format: :json } do
namespace :v1 do
mount_devise_token_auth_for 'User', at: 'auth', skip: [:omniauth_callbacks] # add this and overide the omniauth callbacks
post 'social_auth/callback', to: 'social_auth_controller#authenticate_social_auth_user' # this is the line where we add our routes
end
end
# Api Controller to handle our call backs
require 'json'
class Api::V1::SocialAuthController < ApplicationController
def authenticate_social_auth_user
# params is the response I receive from the client with the data from the provider about the user
@user = User.signin_or_create_from_provider(params) # this method add a user who is new or logins an old one
if @user.persisted?
# I log the user in at this point
sign_in(@user)
# after user is loggedIn, I generate a new_token here
config.session_store :cookie_store, key: '_interslice_session'
config.middleware.use ActionDispatch::Cookies # Required for all session management
config.middleware.use ActionDispatch::Session::CookieStore, config.session_options
# I added this for cors to be able to accept request from client. for now it accepts from all origins
config.middleware.use Rack::Cors do
allow do
origins '*'
resource '*',
headers: :any,
expose: %w[access-token expired token-type uid client],
# Please find this find via config/initializers/devise.rb
# Add these to this file in case you're using these providers
config.omniauth :google_oauth2,
Rails.application.credentials[:google_app_id],
Rails.application.credentials[:google_app_secret],
{ scope: 'userinfo.email, userinfo.profile', skip_jwt: true }
config.omniauth :facebook,
Rails.application.credentials[:facebook_app_id],
Rails.application.credentials[:facebook_app_secret],
// add these gems and run bundle i
gem 'omniauth'
gem 'omniauth-facebook'
gem 'omniauth-google-oauth2'
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
# for omniauth token if its what you're using for your api, I am assuming its what you're using in this tutorial
include DeviseTokenAuth::Concerns::User
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable,
:confirmable, :omniauthable,
omniauth_providers: %i[google_oauth2 facebook] # add omniauthable field and add the providers under omniauth_providers
@NicholusMuwonge
NicholusMuwonge / App.js
Created October 24, 2020 13:54
Social Login example
import React from "react";
import { GoogleAPI, GoogleLogin, GoogleLogout } from "react-google-oauth";
import FacebookLogin from 'react-facebook-login';
function App() {
return (
<div className="App">
<div className="App-header">
@NicholusMuwonge
NicholusMuwonge / Cyclic Rotation
Created October 31, 2019 09:25
Cyclic Rotation, replace the 1st number in the list with the last one in the list a given number of times
An array A consisting of N integers is given. Rotation of the array means that each element is shifted right by one index, and the last element of the array is moved to the first place. For example, the rotation of array A = [3, 8, 9, 7, 6] is [6, 3, 8, 9, 7] (elements are shifted right by one index and 6 is moved to the first place).
The goal is to rotate array A K times; that is, each element of A will be shifted to the right K times.
Write a function:
def solution(A, K)
that, given an array A consisting of N integers and an integer K, returns the array A rotated K times.
@NicholusMuwonge
NicholusMuwonge / missing number
Created October 31, 2019 06:56
show unpaired numbers
Task description A non-empty array A consisting of N integers is given.
The array contains an odd number of elements, and each element of the array can be paired with another element that has the same value, except for one element that is left unpaired. For example, in array A such that: A[0] = 9 A[1] = 3 A[2] = 9 A[3] = 3 A[4] = 9 A[5] = 7 A[6] = 9 the elements at indexes 0 and 2 have value 9, the elements at indexes 1 and 3 have value 3, the elements at indexes 4 and 6 have value 9, the element at index 5 has value 7 and is unpaired. Write a function: def solution(A) that, given an array A consisting of N integers fulfilling the above conditions, returns the value of the unpaired element. For example, given array A such that: A[0] = 9 A[1] = 3 A[2] = 9 A[3] = 3 A[4] = 9 A[5] = 7 A[6] = 9 the function should return 7, as explained in the example above. Write an efficient algorithm for the following assumptions: N is an odd integer within the range [1..1,000,000]; each element of