Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement authentication and user modules #9

Open
wants to merge 123 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
123 commits
Select commit Hold shift + click to select a range
5e44c4d
Change versions of welcom module
Iwasherd Apr 10, 2019
622a533
Merge branch 'master' of https://github.com/sysgears/restapp
Iwasherd Apr 19, 2019
f020440
Create request middleware
Iwasherd May 7, 2019
fcba3ef
Add locales
Iwasherd May 7, 2019
e65edd9
Add auth base components
Iwasherd May 7, 2019
907aad7
Add Data root component
Iwasherd May 7, 2019
84ed707
Add config
Iwasherd May 7, 2019
236aff7
Add universalcoockie interface
Iwasherd May 7, 2019
4393437
Add register functionality with reducers
Iwasherd May 7, 2019
e404dda
Add register functionality for mobile app
Iwasherd May 7, 2019
0f369c2
Update ServerModule
Sasha-belokur May 7, 2019
c5993f4
Add initial setup for server user module
Sasha-belokur May 7, 2019
96a444c
Update createServerApp
Sasha-belokur May 7, 2019
83185c7
Add emailTemplate
Sasha-belokur May 7, 2019
05e59c7
Add password sub-module
Sasha-belokur May 7, 2019
90c5115
Add bodyParser for app
Sasha-belokur May 7, 2019
2d62b17
Merge branch 'user/sign-up' of github.com:sysgears/restapp into user/…
Sasha-belokur May 7, 2019
d2f2a5f
Change form error handler
Iwasherd May 7, 2019
1b119b3
Merge branch 'user/sign-up' of github.com:sysgears/restapp into user/…
Sasha-belokur May 7, 2019
79cea34
Add status for errors response in register
Sasha-belokur May 7, 2019
1cd7ed2
Fix typo in migrations
Sasha-belokur May 7, 2019
65e6ab8
Fix redux middleware
Iwasherd May 7, 2019
c96ad9d
Merge branch 'user/sign-up' of https://github.com/sysgears/restapp in…
Iwasherd May 7, 2019
be7ded2
Add auth module
Sasha-belokur May 7, 2019
5ed25af
Add types for passport
Sasha-belokur May 7, 2019
72b4ba9
Add config for session and jwt
Sasha-belokur May 7, 2019
dfda04a
Add login conrtoller
Sasha-belokur May 7, 2019
08e2d22
Fix validation errors
Iwasherd May 7, 2019
a7e0c7d
Remove social auth
Sasha-belokur May 7, 2019
acc851e
Update types for auth module
Sasha-belokur May 7, 2019
54a24a0
Merge branch 'user/sign-up' into user/sign-up-and-login
Iwasherd May 7, 2019
24b0845
Update LocalStrategy usernameField
Sasha-belokur May 7, 2019
9438b34
Update errors response
Sasha-belokur May 7, 2019
b76267c
Merge branch 'user/sign-up' of github.com:sysgears/restapp into user/…
Sasha-belokur May 7, 2019
9f9dba8
Merge branch 'user/sign-up' into user/sign-up-and-login
Iwasherd May 7, 2019
66cbb63
Merge branch 'user/sign-up-and-login' of https://github.com/sysgears/…
Iwasherd May 7, 2019
71645c1
Add uthentication module
Iwasherd May 7, 2019
4453d8c
Add httpClient in Client module and create request handler
Iwasherd May 8, 2019
e012105
Add login and logout components
Iwasherd May 8, 2019
8abe1f6
Add authentication to modules
Sasha-belokur May 8, 2019
2575223
Add controller for current user
Sasha-belokur May 8, 2019
45fd75f
Fix handle response
Iwasherd May 8, 2019
2ec0ad9
Merge branch 'user/sign-up-and-login' of https://github.com/sysgears/…
Iwasherd May 8, 2019
231245b
Add loading component and Fix login with logout
Iwasherd May 9, 2019
e6d1d91
Create react-native redux middleware
Iwasherd May 9, 2019
b66bfe2
Add react-native login
Iwasherd May 9, 2019
9734284
Update errors response in login
Sasha-belokur May 10, 2019
6de1a0e
Add response for session logout
Sasha-belokur May 10, 2019
866b83f
Merge branch 'user/sign-up-and-login' of github.com:sysgears/restapp …
Sasha-belokur May 10, 2019
92a089e
Fix throw
Iwasherd May 10, 2019
93ee909
Merge branch 'user/sign-up-and-login' of https://github.com/sysgears/…
Iwasherd May 10, 2019
7f6d886
Add controllers for profile
Sasha-belokur May 10, 2019
8d867e1
Move redux middleware in authentication jwt module and add requests f…
Iwasherd May 10, 2019
eded36d
Update controllers
Sasha-belokur May 13, 2019
d720d5d
Merge branch 'user/profile' of github.com:sysgears/restapp into user/…
Sasha-belokur May 13, 2019
7f69b5c
Rename redux middleware
Iwasherd May 13, 2019
4bb5ebb
Fix get users and users filters
Iwasherd May 13, 2019
d9b694e
Remove extra property in user conrtoller
Sasha-belokur May 13, 2019
da85106
Merge branch 'user/profile' of github.com:sysgears/restapp into user/…
Sasha-belokur May 13, 2019
be5a69e
Fix add and edit user
Iwasherd May 13, 2019
454c129
Fix react-native user interface
Iwasherd May 13, 2019
9591392
Add try catch for form validations
Iwasherd May 14, 2019
2cbb167
Add check for accessMiddleware
Sasha-belokur May 14, 2019
658c71c
Add forgot and reset password conrtollers
Sasha-belokur May 14, 2019
42970ec
Moved passport loging middleware to auth module
Sasha-belokur May 14, 2019
6243240
Merge branch 'user/profile' of github.com:sysgears/restapp into user/…
Sasha-belokur May 14, 2019
7e85182
Update session accessMiddleware response
Sasha-belokur May 14, 2019
aedd4fa
Add common redux middleware for async operation and middleware in jwt…
Iwasherd May 14, 2019
5b16e3f
removed extra features
Iwasherd May 14, 2019
4e6c08f
Reaname callAPI to APICall
Iwasherd May 14, 2019
f58a58e
Fix prew commit
Iwasherd May 15, 2019
a099c75
Update controllers with identity check
Sasha-belokur May 15, 2019
b3ec2b1
Remove table auth_certificate from migrations
Sasha-belokur May 15, 2019
8095418
Fix typo in user conrtollers
Sasha-belokur May 15, 2019
c63245b
Add reset and forgot password components
Iwasherd May 15, 2019
d43956b
Add routes and navigations screens for forgot and reset password
Iwasherd May 15, 2019
3f21abd
Add social to user and auth modules
Sasha-belokur May 15, 2019
5bf50f0
Merge branch 'user/profile' of github.com:sysgears/restapp into user/…
Sasha-belokur May 15, 2019
ff7e86d
Fix invalid refresh token response
Sasha-belokur May 15, 2019
db05f63
Divide the storage into several
Iwasherd May 16, 2019
9a38a0d
Clear data
Iwasherd May 16, 2019
886b611
Add request on currentUser in AuthRoute
Iwasherd May 16, 2019
30cb626
Implement working socils with jwt module
Iwasherd May 16, 2019
d227a1e
Fix social auth with jwt
Iwasherd May 17, 2019
7cb9828
Fix socials with session
Iwasherd May 20, 2019
dd6a25c
Clear data
Iwasherd May 20, 2019
8e91567
Change interfaces
Iwasherd May 20, 2019
38be3a8
Move session settings to settings module
Iwasherd May 20, 2019
f568b2d
Fix check on empty tokens
Iwasherd May 20, 2019
84db025
Add interface for user action creators
Iwasherd May 20, 2019
fb53859
Add and Fix Profile on react-native app
Iwasherd May 20, 2019
6850d25
Fix login valiadtion errors in Login component
Iwasherd May 21, 2019
3cf8827
Change api url
Iwasherd May 21, 2019
7bea2fc
Fix Set redux store data after 401 response
Iwasherd May 21, 2019
b4549c3
Fix reset pass when editing user
Iwasherd May 22, 2019
5235f6c
Remove InputItem from server module
Iwasherd May 22, 2019
585d3b3
Fix check on empty tokens
Iwasherd May 22, 2019
32eab84
Fix UserShapePassword interface
Iwasherd May 22, 2019
f2cbfb0
Clear unnecessary try catch
Iwasherd May 23, 2019
3d0d1d3
Re-write UserEdit component on functional component
Iwasherd May 23, 2019
0e70bfe
Fix sending 401 status
Iwasherd May 28, 2019
0f64679
Merge branch 'master' of https://github.com/sysgears/restapp
Iwasherd May 28, 2019
160b198
Merge with master and fixing some mistakes
Iwasherd May 29, 2019
e0028d0
Fix redirect from auth routes
Iwasherd May 29, 2019
8c6c0d9
Separation of a user module into signUp, profile and users modules
Iwasherd May 30, 2019
701b2a6
Create MetaData component
Iwasherd May 31, 2019
b41a755
Move common types in types directory
Iwasherd May 31, 2019
a20423e
Fix prev commit
Iwasherd May 31, 2019
7615835
Cleanup
gekanchiko Jun 6, 2019
d5a9c21
Rename action functions
Iwasherd Jun 6, 2019
12c1486
Merge branch 'master' of https://github.com/sysgears/restapp
Iwasherd Jun 7, 2019
e0a0d55
Merge with master
Iwasherd Jun 7, 2019
83f442b
Fix prew commit
Iwasherd Jun 7, 2019
e964240
Move MetaData to look module
Iwasherd Jun 7, 2019
916beb1
Revert "Fix prew commit"
Iwasherd Jun 7, 2019
5a6b803
Update yarn lock
Iwasherd Jun 7, 2019
40560ba
Fix yarn.lock
Iwasherd Jun 7, 2019
308f1c2
Add confirmation api route
basilalex Jun 27, 2019
5cdd618
Update confirmation link
basilalex Jun 27, 2019
1852195
Fix type of isActive argument
basilalex Jun 27, 2019
d362d22
Clear env
basilalex Jun 27, 2019
0dcb21a
Fix forgotPassword controller
basilalex Jun 27, 2019
709291e
Fix broken link
basilalex Jun 27, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ jsconfig.json
public
*.iml
.npmrc
packages/server/sessions
53 changes: 53 additions & 0 deletions config/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
export default {
secret: process.env.NODE_ENV === 'test' ? 'secret for tests' : process.env.AUTH_SECRET,
session: {
enabled: false,
secret: 'secret',
store: null,
cookie: { maxAge: 60000 },
resave: false,
saveUninitialized: false
},
jwt: {
enabled: true,
tokenExpiresIn: '1m',
refreshTokenExpiresIn: '7d'
},
password: {
requireEmailConfirmation: true,
sendPasswordChangesEmail: true,
minLength: 8,
enabled: true
},
social: {
facebook: {
enabled: false,
clientID: process.env.FACEBOOK_CLIENTID,
clientSecret: process.env.FACEBOOK_CLIENTSECRET,
callbackURL: '/api/auth/facebook/callback',
scope: ['email'],
profileFields: ['id', 'emails', 'displayName']
},
github: {
enabled: false,
clientID: process.env.GITHUB_CLIENTID,
clientSecret: process.env.GITHUB_CLIENTSECRET,
callbackURL: '/api/auth/github/callback',
scope: ['user:email']
},
linkedin: {
enabled: false,
clientID: process.env.LINKEDIN_CLIENTID,
clientSecret: process.env.LINKEDIN_CLIENTSECRET,
callbackURL: '/api/auth/linkedin/callback',
scope: ['r_liteprofile']
},
google: {
enabled: false,
clientID: process.env.GOOGLE_CLIENTID,
clientSecret: process.env.GOOGLE_CLIENTSECRET,
callbackURL: '/api/auth/google/callback',
scope: ['https://www.googleapis.com/auth/userinfo.email', 'https://www.googleapis.com/auth/userinfo.profile']
}
}
};
1 change: 1 addition & 0 deletions config/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export { default as app } from './app';
export { default as auth } from './auth';
export { default as db } from './db';
export { default as mailer } from './mailer';
export { default as analytics } from './analytics';
Expand Down
49 changes: 49 additions & 0 deletions modules/authentication/client-react/access/AccessModule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import ClientModule, { ClientModuleShape } from '@restapp/module-client-react';

/**
* Access module which provides authentication functions
*/
export interface AccessModuleShape extends ClientModuleShape {
// login handlers from all authentication modules
login?: Array<(callback?: () => void) => Promise<void>>;
// logout handlers from all authentication modules
logout?: Array<(callback?: () => void) => Promise<void>>;
}

interface AccessModule extends AccessModuleShape {}

class AccessModule extends ClientModule {
/**
* Constructs access module representation, that folds all the feature modules
* into a single module represented by this instance.
*
* @param modules feature modules
*/
constructor(...modules: AccessModuleShape[]) {
super(...modules);
}

/**
* call all methods of login in authentication modules.
*
* @returns merge login methods
*/
public async doLogin(callback?: () => void) {
for (const login of this.login) {
await login(callback);
}
}

/**
* call all methods of logout in authentication modules.
*
* @returns merge logout methods
*/
public async doLogout(callback?: () => void) {
for (const logout of this.logout) {
await logout(callback);
}
}
}

export default AccessModule;
47 changes: 47 additions & 0 deletions modules/authentication/client-react/access/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import * as React from 'react';
import jwt from './jwt';
import session from './session';
import AccessModule from './AccessModule';

interface PageReloaderProps {
children?: React.ReactElement;
}

interface AuthPageReloaderProps extends PageReloaderProps {}

const ref: React.RefObject<any> = React.createRef();

const rerenderApp = () => {
ref.current.reloadPage();
};

const login = async (_clearStore?: () => void) => {
rerenderApp();
};

const logout = async (clearStore: () => void) => {
await clearStore();
rerenderApp();
};

class PageReloader extends React.Component<PageReloaderProps> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should stop using class-based components in favor of functional ones.

public state = {
key: 1
};

public reloadPage() {
this.setState({ key: this.state.key + 1 });
}

public render() {
return React.cloneElement(this.props.children, { key: this.state.key });
}
}

const AuthPageReloader = ({ children }: AuthPageReloaderProps) => <PageReloader ref={ref}>{children}</PageReloader>;

export default new AccessModule(jwt, session, {
dataRootComponent: [AuthPageReloader],
login: [login],
logout: [logout]
});
100 changes: 100 additions & 0 deletions modules/authentication/client-react/access/jwt/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import axios from 'axios';
import { getItem, setItem, removeItem } from '@restapp/core-common/clientStorage';
import { Middleware } from 'redux';
import settings from '../../../../../settings';
import AccessModule from '../AccessModule';

enum TokensEnum {
accessToken = 'accessToken',
refreshToken = 'refreshToken'
}

interface Tokens {
[key: string]: TokensEnum;
}

const saveTokens = async ({ accessToken, refreshToken }: Tokens) => {
await setItem(TokensEnum.accessToken, accessToken);
await setItem(TokensEnum.refreshToken, refreshToken);
};

const removeTokens = async () => {
await removeItem(TokensEnum.accessToken);
await removeItem(TokensEnum.refreshToken);
};

const refreshAccessToken = async () => {
try {
const { data } = await axios.post(`${__API_URL__}/refreshToken`, {
refreshToken: await getItem('refreshToken')
});
if (data) {
const { accessToken, refreshToken } = data;
await saveTokens({ accessToken, refreshToken });
} else {
await removeTokens();
}
} catch (e) {
await removeTokens();
throw e;
}
};

const reduxMiddleware: Middleware = ({ dispatch }) => next => action => {
const { types, status, ...rest } = action;
(async () => {
try {
if (status === 401) {
try {
await refreshAccessToken();
const newAction = { ...action, status: null };
return dispatch(newAction);
} catch (e) {
throw e;
}
}
return next(action);
} catch (e) {
next({
type: types.FAIL,
...rest
});
throw e;
}
})();
};

axios.interceptors.request.use(async config => {
const accessToken = await getItem(TokensEnum.accessToken);

const arrayExceptions = ['login', 'refreshTokens'];
const checkInclude = arrayExceptions.some(exception => config.url.includes(exception));

config.headers = !checkInclude && accessToken ? { Authorization: `Bearer ${accessToken}` } : {};
return config;
});

axios.interceptors.response.use(async (res: any) => {
if (res.config.url.includes('login')) {
if (!!res.data && res.data.tokens) {
const {
data: {
tokens: { accessToken, refreshToken }
}
} = res;
await saveTokens({ accessToken, refreshToken });
res.data = res.data.user;
return res;
} else {
await removeTokens();
}
}
return res;
});

export default (settings.auth.jwt.enabled
? new AccessModule({
logout: [removeTokens],
reduxMiddleware: [reduxMiddleware]
})
: undefined);
12 changes: 12 additions & 0 deletions modules/authentication/client-react/access/session/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import settings from '../../../../../settings';
import AccessModule from '../AccessModule';
import axios from 'axios';

const logout = async () => {
axios.post(`${__API_URL__}/logout`);
};
export default (settings.auth.session.enabled
? new AccessModule({
logout: [logout]
})
: undefined);
12 changes: 12 additions & 0 deletions modules/authentication/client-react/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import url from 'url';
import { Constants } from 'expo';

export default function buildRedirectUrlForMobile(authType: string) {
const { protocol, hostname, port } = url.parse(__WEBSITE_URL__);
const expoHostname = `${url.parse(Constants.linkingUri).hostname}.nip.io`;
const urlHostname = process.env.NODE_ENV === 'production' ? hostname : expoHostname;

return `${protocol}//${urlHostname}${port ? ':' + port : ''}/api/auth/${authType}?expoUrl=${encodeURIComponent(
Constants.linkingUri
)}`;
}
5 changes: 5 additions & 0 deletions modules/authentication/client-react/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import authentication from './access';

export * from './social';

export default authentication;
8 changes: 8 additions & 0 deletions modules/authentication/client-react/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "@restapp/authentication-client-react",
"version": "0.1.0",
"private": true,
"dependencies": {
"@restapp/core-common": "^0.1.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
.facebookBtn {
min-width: 320px;
margin-top: 10px;
background-color: #3769ae;
border-color: #3769ae;
display: flex;
justify-content: flex-start;
align-items: center;
}

.facebookBtn:hover {
background-color: #17427e;
border-color: #17427e;
}

.iconContainer {
display: flex;
flex: 1 1 10%;
justify-content: flex-start;
align-items: center;
}

.separator {
height: 28px;
width: 1px;
background-color: #fff !important;
margin-left: 10px;
}

.btnText {
display: flex;
justify-content: flex-start;
flex: 5
}

.facebookIcon {
color: #fff;
font-size: 30px;
}
Loading