Skip to content

Instantly share code, notes, and snippets.

View saurabhnative's full-sized avatar
🎯
Focusing

Saurabh Mhatre saurabhnative

🎯
Focusing
View GitHub Profile
function Header(props) {
...
function renderLogout() {
if(props.location.pathname === '/home'){
return(
<div className="ml-auto">
<button className="btn btn-danger" onClick={() => handleLogout()}>Logout</button>
</div>
)
}
const jwt = require("jsonwebtoken");
const User = require('../models/User');
module.exports = async function(req, res, next) {
const token = req.header("token");
if (!token) return res.status(401).json({ message: "Auth Error" });
try {
const decoded = jwt.verify(token, "randomString");
req.user = decoded.user;
const user = await User.findById(decoded.user.id);
function Home(props) {
useEffect(() => {
axios.get(API_BASE_URL+'/user/me', { headers: { 'token': localStorage.getItem(ACCESS_TOKEN_NAME) }})
.then(function (response) {
if(response.status !== 200){
redirectToLogin()
}
})
.catch(function (error) {
redirectToLogin()
...
import PrivateRoute from './utils/PrivateRoute';
...
<Switch>
.....
<PrivateRoute path="/home">
<Home/>
</PrivateRoute>
</Switch>
import React from 'react';
import { Redirect, Route } from "react-router-dom";
import { ACCESS_TOKEN_NAME } from '../constants/apiContants';
function PrivateRoute({ children, ...rest }) {
return (
<Route
{...rest}
render={({ location }) =>
localStorage.getItem(ACCESS_TOKEN_NAME) ? (
children
if(response.status === 200){
setState(prevState => ({
...prevState,
'successMessage' : 'Registration successful. Redirecting to home page..'
}))
localStorage.setItem(ACCESS_TOKEN_NAME,response.data.token);
redirectToHome();
props.showError(null)
}
import React, {useState} from 'react';
import axios from 'axios';
function App() {
...
const handleInputSubmission = () => {
if(selectedObject && Object.keys(selectedObject).length === 7) {
...
axios.post(process.env.REACT_APP_HEROKU_SERVER_URL, selectedObject)
.then(function (response) {
setPredictionLoading(false)
import React,{ useState } from 'react';
...
function OptionSelection({itemKey, setOptionInObject}) {
const [currentSelectedOption, setSelectedOption] = useState(null);
const handleDropDownSelection = (consoleOption) => {
setSelectedOption(consoleOption)
setOptionInObject(itemKey, consoleOption)
}
...
}
import optionSources from '../optionsSources.json';
function OptionSelection({itemKey, setOptionInObject}) {
...
const renderOptionsDropdown = () => {
const selectionOptions = optionSources[itemKey].options;
return selectionOptions.map((selectionOption, index)=>{
return (
<div className="dropdown-item pointer"
key={`${index}${selectionOption}`}
onClick={() => handleDropDownSelection(selectionOption)}
import React,{ useState } from 'react';
import optionSources from '../optionsSources.json';
function OptionSelection({itemKey, setOptionInObject}) {
const title = optionSources[itemKey].dropDownPlaceholder;
const icon = optionSources[itemKey].icon;
return(
<div className="d-flex justify-content-start align-items-center mt-2 selection-item">
<div className="option-label">
<b><span role="img" aria-label="label-icon">{icon}</span>{` ${title}`}</b>
</div>