Skip to content

Instantly share code, notes, and snippets.

Avatar
🏠
Working from home

Ali Eslamifard alieslamifard

🏠
Working from home
View GitHub Profile
View index.jsx
import React from 'react';
import Link from 'next/link';
export default function HomePage() {
return (
<div>
<h2>Hello World!</h2>
<Link href="/dashboard">Go to Dashboard</Link>
</div>
);
View dashboard.jsx
import React from 'react';
import withPrivateRoute from '../components/withPrivateRoute';
const Dashboard = () => {
return <div>This is a Dashboard page which is private.</div>;
};
Dashboard.getInitialProps = async props => {
console.info('##### Congratulations! You are authorized! ######', props);
return {};
@alieslamifard
alieslamifard / withPrivateRoute.jsx
Last active Mar 30, 2021
Private route HOC for Next.js
View withPrivateRoute.jsx
import React from 'react';
import Router from 'next/router';
const login = '/login?redirected=true'; // Define your login route address.
/**
* Check user authentication and authorization
* It depends on you and your auth service provider.
* @returns {{auth: null}}
*/
View gist:559448f4448200f21acdf561c38f681e
const [installationStatus, installationEvent] = useInstallPrompt(true);
useEffect(() => {
// installationStatus is one of these states: null/dismissed/accepted/installed
if(installationStatus === 'accepted') {
// User accept it. You can save it in some log with device config.
}
// Show Add-to-home-screen prompt, if user have it's condition.
if(installationEvent) {
View gist:80668a1a4206f849c104354b014c55c0
useEffect(() => {
console.log(Device());
})
// {
// "browser": {
// "name": "Safari",
// "version": "11.0"
// },
// "os": {
View gist:a826c221b58438f31898916facdcd92d
// npm install react-pwa-toolkit
import React from "react";
import Device, { useNetwork, usePwa, useVisibility, useInstallPrompt } from "react-pwa-toolkit";
View yeoman-7.js
'use strict';
const Generator = require('yeoman-generator');
const chalk = require('chalk');
const yosay = require('yosay');
module.exports = class extends Generator {
// Note: arguments and options should be defined in the constructor.
constructor(args, opts) {
super(args, opts);