Skip to content

Commit

Permalink
Merge pull request #664 from cozy/auth-component-npm
Browse files Browse the repository at this point in the history
Auth component published on npm
  • Loading branch information
ptbrowne authored Jan 9, 2018
2 parents 4a5dbcd + 6ce5b4d commit 7c60fc9
Show file tree
Hide file tree
Showing 18 changed files with 7,220 additions and 46 deletions.
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules/**
src/authentication/node_modules/**
src/authentication/build/**
8 changes: 4 additions & 4 deletions config/webpack.config.cozy-ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const ExtractTextPlugin = require('extract-text-webpack-plugin')

module.exports = {
module.exports = (production) => ({
resolve: {
extensions: ['.styl']
},
Expand All @@ -20,7 +20,7 @@ module.exports = {
{
loader: 'css-loader',
options: {
sourceMap: true,
sourceMap: !production,
importLoaders: 1,
modules: true,
localIdentName: '[local]--[hash:base64:5]'
Expand All @@ -29,7 +29,7 @@ module.exports = {
{
loader: 'postcss-loader',
options: {
sourceMap: true,
sourceMap: !production,
plugins: () => [
require('autoprefixer')({ browsers: ['last 2 versions'] })
]
Expand All @@ -46,4 +46,4 @@ module.exports = {
}
]
}
}
})
52 changes: 29 additions & 23 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,17 @@
"main": "src/main.jsx",
"scripts": {
"build:drive": "npm run build:drive:browser",
"build:drive:browser":
"npm run internal:build -- --env.app=drive --env.target=browser --env.production",
"build:drive:mobile":
"npm run internal:build -- --env.app=drive --env.target=mobile --env.production",
"build:drive:browser": "npm run internal:build -- --env.app=drive --env.target=browser --env.production",
"build:drive:mobile": "npm run internal:build -- --env.app=drive --env.target=mobile --env.production",
"build:photos": "npm run build:photos:browser",
"build:photos:browser":
"npm run internal:build -- --env.app=photos --env.target=browser --env.production",
"build:photos:browser": "npm run internal:build -- --env.app=photos --env.target=browser --env.production",
"clean:drive:browser": "rm -rf build/drive/*",
"clean:photos:browser": "rm -rf build/photos/*",
"clean:mobile": "rm -rf targets/*/mobile/www/*",
"deploy:drive":
"git-directory-deploy --directory=build/drive/ --username=Cozy --email=contact@cozycloud.cc --repo=https://$GITHUB_TOKEN@github.com/$TRAVIS_REPO_SLUG.git",
"deploy:photos":
"git-directory-deploy --directory=build/photos/ --username=Cozy --email=contact@cozycloud.cc --repo=https://$GITHUB_TOKEN@github.com/$TRAVIS_REPO_SLUG.git",
"deploy:drive": "git-directory-deploy --directory=build/drive/ --username=Cozy --email=contact@cozycloud.cc --repo=https://$GITHUB_TOKEN@github.com/$TRAVIS_REPO_SLUG.git",
"deploy:photos": "git-directory-deploy --directory=build/photos/ --username=Cozy --email=contact@cozycloud.cc --repo=https://$GITHUB_TOKEN@github.com/$TRAVIS_REPO_SLUG.git",
"internal:build": "webpack --config webpack.config.js --bail",
"internal:watch":
"webpack --config webpack.config.js --watch --display-modules --display-chunks",
"internal:watch": "webpack --config webpack.config.js --watch --display-modules --display-chunks",
"tx": "tx pull --all || true",
"preinternal:build": "npm-run-all lint tx",
"prebuild:drive:browser": "npm run clean:drive:browser",
Expand All @@ -36,12 +30,9 @@
"precommit": "lint-staged",
"test": "node_modules/.bin/jest --coverage --config=jest.config.json",
"watch:drive": "npm run watch:drive:browser",
"watch:drive:browser":
"npm run internal:watch -- --env.app=drive --env.target=browser",
"watch:drive:mobile":
"npm run internal:watch -- --env.app=drive --env.target=mobile",
"watch:drive:standalone":
"webpack-dev-server --config webpack.config.js --env.app=drive --env.target=mobile --port 8084 --inline --hot",
"watch:drive:browser": "npm run internal:watch -- --env.app=drive --env.target=browser",
"watch:drive:mobile": "npm run internal:watch -- --env.app=drive --env.target=mobile",
"watch:drive:standalone": "webpack-dev-server --config webpack.config.js --env.app=drive --env.target=mobile --port 8084 --inline --hot",
"watch:photos": "npm run watch:photos:browser",
"watch:photos:browser": "npm run internal:watch -- --env.app=photos --env.target=browser",
"genicon:drive:mobile": "(cd targets/drive/mobile && rm -Rf ./res/icons ./res/screens && splashicon-generator --imagespath='./res/model')",
Expand Down Expand Up @@ -171,15 +162,23 @@
"redux-logger": "^3.0.6",
"redux-mock-store": "^1.2.3",
"redux-raven-middleware": "^1.2.0",
"redux-thunk": "^2.1.0"
"redux-thunk": "^2.1.0",
"webpack-node-externals": "^1.6.0"
},
"jest": {
"moduleFileExtensions": ["js", "jsx", "json", "styl"],
"moduleFileExtensions": [
"js",
"jsx",
"json",
"styl"
],
"moduleNameMapper": {
"\\.(png|gif|jpe?g|svg)$": "<rootDir>/test/__mocks__/fileMock.js",
"styles": "identity-obj-proxy"
},
"transformIgnorePatterns": ["node_modules/(?!cozy-ui)"],
"transformIgnorePatterns": [
"node_modules/(?!cozy-ui)"
],
"globals": {
"__ALLOW_HTTP__": false,
"__TARGET__": "browser",
Expand All @@ -197,7 +196,11 @@
"prettier/react",
"prettier/standard"
],
"plugins": ["react", "prettier", "standard"],
"plugins": [
"react",
"prettier",
"standard"
],
"parser": "babel-eslint",
"parserOptions": {
"ecmaFeatures": {
Expand All @@ -224,6 +227,9 @@
"singleQuote": true
},
"lint-staged": {
"{src,test}/**/*.{js,jsx}": ["prettier --write", "git add"]
"{src,test}/**/*.{js,jsx}": [
"prettier --write",
"git add"
]
}
}
21 changes: 14 additions & 7 deletions src/authentication/Authentication.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import React, { Component } from 'react'

import PropTypes from 'prop-types'
import Welcome from './steps/Welcome'
import SelectServer from './steps/SelectServer'

import { logException } from 'drive/mobile/lib/reporter'

const STEP_WELCOME = 'STEP_WELCOME'
const STEP_EXISTING_SERVER = 'STEP_EXISTING_SERVER'

Expand Down Expand Up @@ -37,19 +35,20 @@ class Authentication extends Component {
}

connectToServer = async url => {
const { onComplete, onException, router } = this.props
try {
this.setState({ generalError: null, fetching: true })
const cozyClient = this.context.client
const { client, token } = await cozyClient.register(url)
this.props.onComplete({
onComplete({
url,
clientInfo: client,
token,
router: this.props.router
clientInfo: client,
router: router
})
} catch (err) {
this.setState({ generalError: err })
logException(err, {
onException(err, {
tentativeUrl: url,
onboardingStep: 'connecting to server'
})
Expand All @@ -59,6 +58,7 @@ class Authentication extends Component {
}

render() {
const { onException } = this.props
const { currentStepIndex, generalError, fetching } = this.state
const currentStep = this.steps[currentStepIndex]

Expand All @@ -78,6 +78,7 @@ class Authentication extends Component {
previousStep={() => this.onAbort()}
externalError={generalError}
fetching={fetching}
onException={onException}
/>
)
default:
Expand All @@ -86,4 +87,10 @@ class Authentication extends Component {
}
}

Authentication.propTypes = {
onComplete: PropTypes.func.isRequired,
onException: PropTypes.func.isRequired,
router: PropTypes.object
}

export default Authentication
9 changes: 8 additions & 1 deletion src/authentication/MobileRouter.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Router, withRouter } from 'react-router'

import Authentication from './Authentication'
import Revoked from './Revoked'
import { logException } from 'drive/mobile/lib/reporter'

const MobileRouter = ({
history,
Expand All @@ -13,7 +14,13 @@ const MobileRouter = ({
onLogout
}) => {
if (!isAuthenticated) {
return <Authentication router={history} onComplete={onAuthenticated} />
return (
<Authentication
router={history}
onComplete={onAuthenticated}
onException={logException}
/>
)
} else if (isRevoked) {
return (
<Revoked
Expand Down
55 changes: 55 additions & 0 deletions src/authentication/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
[Cozy] Authentication component
==================

What's cozy-authentication
----------------

Cozy-Authentication is a React component used to authenticate to a Cozy. It acts as a login page.

What's Cozy?
------------

![Cozy Logo](https://cdn.rawgit.com/cozy/cozy-guidelines/master/templates/cozy_logo_small.svg)

[Cozy] is a platform that brings all your web services in the same private space. With it, your webapps and your devices can share data easily, providing you with a new experience. You can install Cozy on your own hardware where no one's tracking you.

Contribute
----------

If you want to work on cozy-authentication itself and submit code modifications, feel free to open pull-requests! See the [contributing guide][contribute] for more information about this repository structure, testing, linting and how to properly open pull-requests.


Community
---------

### Get in touch

You can reach the Cozy Community by:

- Chatting with us on IRC [#cozycloud on Freenode][freenode]
- Posting on our [Forum][forum]
- Posting issues on the [Github repos][github]
- Say Hi! on [Twitter][twitter]


Licence
-------

cozy-authentication is developed by Cozy Cloud and distributed under the [MIT].


[cozy]: https://cozy.io "Cozy Cloud"
[setup]: https://dev.cozy.io/#set-up-the-development-environment "Cozy dev docs: Set up the Development Environment"
[doctypes]: https://dev.cozy.io/#main-document-types
[bill-doctype]: https://github.com/cozy-labs/konnectors/blob/master/server/models/bill.coffee
[konnector-doctype]: https://github.com/cozy-labs/konnectors/blob/master/server/models/konnector.coffee
[konnectors]: https://github.com/cozy-labs/konnectors
[MIT]: https://opensource.org/licenses/MIT
[contribute]: CONTRIBUTING.md
[freenode]: http://webchat.freenode.net/?randomnick=1&channels=%23cozycloud&uio=d4
[forum]: https://forum.cozy.io/
[github]: https://github.com/cozy/
[twitter]: https://twitter.com/mycozycloud
[mocha]: https://mochajs.org/
[should]: npmjs.com/package/should
[checkbox]: https://help.github.com/articles/basic-writing-and-formatting-syntax/#task-lists
10 changes: 9 additions & 1 deletion src/authentication/Revoked.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* global cozy */

import React, { Component } from 'react'
import PropTypes from 'prop-types'
import Modal from 'cozy-ui/react/Modal'
import { translate } from 'cozy-ui/react/I18n'

Expand Down Expand Up @@ -37,9 +38,16 @@ class Revoked extends Component {
primaryAction={() => {
this.logBackIn()
}}
withCross={false}
closable={false}
/>
)
}
}

Revoked.propTypes = {
onLogout: PropTypes.func.isRequired,
onLogBackIn: PropTypes.func.isRequired,
router: PropTypes.object
}

export default translate()(Revoked)
32 changes: 32 additions & 0 deletions src/authentication/Standalone.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import React from 'react'
import { I18n } from 'cozy-ui/react'
import Authentication from './Authentication'
import Revoked from './Revoked'
import PropTypes from 'prop-types'

const withLocales = Wrapped => {
const Wrapper = (props, context) => {
const { lang } = context
// We pluck a subset of the locales not to ship all Drive locales
// when we distribute our component
const locales = require(`!!./json-pluck-loader?key=mobile.onboarding;mobile.revoked!drive/locales/${lang}.json`)
return (
// Wrap into its own I18n to provide its own locales
<I18n dictRequire={() => locales} lang={lang}>
<Wrapped {...props} />
</I18n>
)
}

Wrapper.contextTypes = {
lang: PropTypes.string.isRequired
}

return Wrapper
}

const StandaloneAuthentication = withLocales(Authentication)
const StandaloneRevoked = withLocales(Revoked)

export { StandaloneAuthentication as Authentication }
export { StandaloneRevoked as Revoked }
50 changes: 50 additions & 0 deletions src/authentication/json-pluck-loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* Plucks a JSON
*
* en.json
*
* ```
* {
* a: 1,
* b: {
* c: {
* d: 4
* },
* e: 5
* }
* }
* ```
*
* `json-pluck-loader?key=b.c!en.json` result will be :
*
* ```{ b: { c: { d: 4 }}}```
*
* `json-pluck-loader?key=a!en.json` result will be :
*
* ```{ a: 1}```
*
*/

const { getOptions } = require('loader-utils')

module.exports = function loader(source) {
const options = getOptions(this)
const res = {}
const all = JSON.parse(source)

const paths = options.key.split(';')
for (let i = 0; i < paths.length; i++) {
const path = paths[i].split('.')
const l = path.length
let leaf = all
let curRes = res
for (var j = 0; j < l; j++) {
const token = path[j]
leaf = leaf[token]
curRes[token] = j === l - 1 ? leaf : curRes[token] || {}
curRes = curRes[token]
}
}

return `module.exports = ${JSON.stringify(res)}`
}
Loading

0 comments on commit 7c60fc9

Please sign in to comment.