TW Reporter Registration Package
Host Project Environment
- The host project need to contain following dependency:
- react
- redux
- react-router
- express server
Installation
npm i --save @twreporter/registration
Main Features
- Registration System
- Service/Bookmark widgets
Work Flow
Overview
- oAuth
- twreporter account
- User signin/up at registration page
- User want to bookmark page without signing in.
- The internal data process is same, no matter user sign in or sign up
- In either way (oAuth/twreporter account), user will browse activation page to be redirected to destination.
Sign In/Up twreporter account
- Sign In
- Redirect to confirm page
- User get email and click the url
- Go to activation page
- Click Bookmark
- Redirect to Sign In page (At the time, url contain with query path)
- User signIn and get email. (Store path into localStorage)
- User Click url to activation page
- Activation page get path from localStorage and redirect user back to article page
Sign In with oAuth(Facebook, Google)
- User Sign in.
- Data from front-end App -> API -> Database
- API return auth info and redirect page to home page.
- medium of auth info is cookie.
- order of medium
- cookie
- redux state
- local storage
Registration
- Set up configuration for registration
- Process cookie in oAuth procedure
import { configureAction, authUserAction } from '@twreporter/registration'
if (path === `/${ACTIVATE_PAGE_PATH}`) {
const authInfoString = get(req, 'cookies.auth_info', '')
const authType = get(req, 'query.login', 'email signin')
if (authInfoString) {
const authInfoObj = JSON.parse(authInfoString)
const jwt = get(authInfoObj, 'jwt', '')
if (jwt) {
store.dispatch(authUserAction(authType, authInfoObj))
}
}
}
store.dispatch(configureAction(config.registrationConfigure))
Entry point of application - App.js
const { ifAuthenticated, authInfo, authType, deletAuthInfoAction } = this.props
if(ifAuthenticated && authInfo && (authType === 'facebook' || authType === 'google')) {
setupTokenInLocalStorage(authInfo, localStorageKeys.authInfo)
deletAuthInfoAction()
}
const { authConfigure, renewToken } = this.props
const authInfoString = getItem(localStorageKeys.authInfo)
if(authInfoString) {
const authObj = JSON.parse(authInfoString)
const { apiUrl, renew } = authConfigure
renewToken(apiUrl, renew, authObj)
scheduleRenewToken(
6,
() => {
if (getItem(localStorageKeys.authInfo)) {
renewToken(apiUrl, renew, JSON.parse(getItem(localStorageKeys.authInfo)))
}
}
)
}
const { authUserByTokenAction } = this.props
authUserByTokenAction(7, authType)
Widget
The package provide two kind of widgets. One is bookmark widget, another one is service widgets. Each widget can be served on specific url, so you can use iframe to implement the function.
Data Structure
const bookmarkData = {
slug: 'fake-slug',
host: 'http://fake-host:3000',
is_external: false,
title: 'fake-title',
desc: 'fake-description',
thumbnail: 'fake-thumbnail',
category: 'issue',
published_date: '2017-12-12T08:00:00+08:00',
}
const bookmarkPostMessage = {
bookmarkData,
svgColor: 'white',
}
const bookmarkPostMessage = {
svgColor: 'white',
}
Usage
- Create Receiver Component which use imported WidgetFrame from this package.( which is iframe)
- Receiver Component is equivalent to iframe Service Provider
- Create iFrame in the application page.
Service Provider
const isJson = (string) => {
try {
JSON.parse(string)
} catch (e) {
return false
}
return true
}
class WidgetPrototype extends React.Component {
constructor(props) {
super(props)
this.state = {
postMessage: {}
}
this.receiveMessage = this._receiveMessage.bind(this)
}
_receiveMessage(event) {
if (event.origin !== 'https://www.twreporter.org' && process.env.NODE_ENV === 'production') {
return
}
let dataObject
const data = _.get(event, 'data', '')
if (typeof data === 'string' && isJson(data)) {
dataObject = JSON.parse(event.data)
this.setState({
postMessage: dataObject
})
}
}
componentDidMount() {
window.addEventListener('message', this.receiveMessage, false)
}
}
export default WidgetPrototype
import { BookmarkWidget } from '@twreporter/registration'
import get from 'lodash/get'
import React from 'react'
import WidgetPrototype from './prototype'
const _ = {
get
}
class BookmarkIframe extends WidgetPrototype {
render() {
const { postMessage } = this.state
return (
<BookmarkWidget
slug={_.get(postMessage, 'bookmarkData.slug', '')}
bookmarkData={_.get(postMessage, 'bookmarkData', {})}
svgColor={_.get(postMessage, 'svgColor', '')}
external
mobile
/>
)
}
}
export default BookmarkIframe
Application
class foo extends React.Component {
componentDidMount() {
const serviceElement = document.getElementById('serviceIcon')
serviceElement.onload = () => {
serviceElement.contentWindow.postMessage(JSON.stringify(servicePostMessage), `${HOST}`)
}
}
render() {
return (
<iFrame
id="serviceIcon"
title="service-widget"
src={`${HOST}/widgets-services`}
scrolling="no"
/>
)
}
}
Development
npm run dev //development mode
npm run build //production mode
//Hard reload development without npm link
CUSTOMER_FOLDER=/task/xxfolder/twreporter npm run dev
//Or you can put @twreporter/registration at same directory with your project folder.