Skip to content

Instantly share code, notes, and snippets.

View mentix02's full-sized avatar
💻
coding away

Manan mentix02

💻
coding away
View GitHub Profile
@mentix02
mentix02 / auth.ts
Last active December 2, 2020 01:17
interface TokenResponse {
token: string;
}
export interface Credentials {
username: string;
password: string;
}
const BASE_URL = "http://localhost:8000";
import React, { useState } from "react";
import { Redirect } from "react-router-dom";
import { useDispatch } from "react-redux";
import { logIn } from "../store/auth/actions";
import { Credentials, getToken } from "../api/auth";
import { useAuthenticated } from "../store/auth/hooks";
function Login() {
import React from "react";
import { Route, Redirect } from "react-router-dom";
import { useAuthenticated } from "../store/auth/hooks";
function PrivateRoute({
children,
...rest
}: {
children: JSX.Element | JSX.Element[];
import { useSelector } from "react-redux";
import { AuthState } from "./types";
export const useAuthenticated = (): boolean =>
useSelector(({ auth }: { auth: AuthState }) => auth.isAuthenticated);
export const useToken = (): string | undefined =>
useSelector(({ auth }: { auth: AuthState }) => auth.token);
import { createLogger } from "redux-logger";
import storage from "redux-persist/lib/storage";
import { persistStore, persistReducer } from "redux-persist";
import { compose, createStore, applyMiddleware } from "redux";
import { rootReducer } from "./reducer";
const persistConfig = {
key: "root",
storage: storage,
import { LOGIN, LOGOUT, LoginAction, LogoutAction } from "./types";
export const logIn = (token: string, username: string): LoginAction => ({
type: LOGIN,
payload: { token, username },
});
export const logOut = (): LogoutAction => ({
type: LOGOUT,
});
import { LOGIN, LOGOUT, AuthState, AuthActionType } from "./types";
const initialState: AuthState = {
isAuthenticated: false,
};
const authReducer = (
state: AuthState = initialState,
action: AuthActionType
): AuthState => {
@mentix02
mentix02 / types.ts
Last active December 1, 2020 23:34
export const LOGIN = "LOGIN";
export const LOGOUT = "LOGOUT";
export interface AuthState {
token?: string;
username?: string;
isAuthenticated: boolean;
}
interface LoginPayload {
from __future__ import annotations
from django.db import models
from django.contrib.auth.models import AbstractUser
class FollowRequest(models.Model):
to_follow = models.ForeignKey(
'user.User', on_delete=models.CASCADE, related_name='requests'
class PostDetailView(DetailView):
model = Post
slug_field = 'slug'
slug_url_kwarg = 'post'
context_object_name = 'post'
template_name = 'blog/post/detail.html'
def get_queryset(self):
return self.model.objects.filter(
publish__day=self.kwargs['day'],