-
Notifications
You must be signed in to change notification settings - Fork 5
/
meteor-oauth.js
171 lines (149 loc) · 5.68 KB
/
meteor-oauth.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import React from 'react'
import { ActivityIndicator, Text, TouchableOpacity, View, Modal, WebView } from 'react-native'
import cheerio from 'cheerio'
import qs from 'qs'
import randomize from 'randomatic'
import Base64 from 'base-64'
import Meteor, { createContainer } from 'react-native-meteor'
import providerConfigs from '../provider-configs'
const styles = {
buttonContainer: { flexDirection: 'row', justifyContent: 'space-around' },
button: { flex: -1 },
buttonLogin: { backgroundColor: '#bbb'},
buttonLogout: { backgroundColor: '#bbb'},
buttonLoginText: { textAlign: 'center', paddingVertical: 12, paddingHorizontal: 40 },
buttonLogoutText: { textAlign: 'center', paddingVertical: 12, paddingHorizontal: 40 },
text: { textAlign: 'center', marginBottom: 15 },
view: { flex: 1, alignSelf: 'stretch', justifyContent: 'center' }
}
class Login extends React.Component {
static propTypes = {
provider: React.PropTypes.string.isRequired,
callbackUrl: React.PropTypes.string.isRequired,
meteorServerDomain: React.PropTypes.string.isRequired,
meteorServerProtocol: React.PropTypes.string.isRequired,
clientId: React.PropTypes.string.isRequired,
scope: React.PropTypes.string,
url: React.PropTypes.string,
extraRequestParams: React.PropTypes.string,
styles: React.PropTypes.object
}
static defaultProps = {
styles: {},
classNames: {}
}
state = {
stage: 'initial',
targetUrl: ''
}
constructor (props) {
super(props)
this.providerConfig = providerConfigs[this.props.provider] || {}
const propStyles = this.props.styles || {}
this.styles = {
buttonContainer: Object.assign({}, styles.buttonContainer, propStyles.buttonContainer),
buttonLogin: Object.assign({}, styles.button, styles.buttonLogin, this.providerConfig.styles.buttonLogin, propStyles.buttonLogin),
buttonLogout: Object.assign({}, styles.button, styles.buttonLogout, this.providerConfig.styles.buttonLogout, propStyles.buttonLogout),
buttonLoginText: Object.assign({}, styles.buttonLoginText, this.providerConfig.styles.buttonLoginText, propStyles.buttonLoginText),
buttonLogoutText: Object.assign({}, styles.buttonLogoutText, this.providerConfig.styles.buttonLogoutText, propStyles.buttonLogoutText),
text: Object.assign({}, styles.text, propStyles.text),
view: Object.assign({}, styles.view, propStyles.view),
}
}
login = () => {
const url = this.props.url || this.providerConfig.url
const randomToken = randomize('*', 10)
const state = stateParam('popup', randomToken, this.props.callbackUrl)
const queryString = qs.stringify(Object.assign({
client_id: this.props.clientId,
redirect_uri: this.props.callbackUrl,
scope: this.props.scope || this.providerConfig.scope,
state
}, this.providerConfig.extraRequestParams, this.props.extraRequestParams))
this.setState({
targetUrl: `${url}?${queryString}`,
stage: 'webView'
})
}
navigation = ({ url }) => {
const regex = new RegExp(`^${this.props.callbackUrl}\[\?#](.*)$`)
const parsedUrl = regex.exec(url)
if (parsedUrl) {
this.setState({ targetUrl : null })
setTimeout(() => {
this.setState({ stage: 'confirmation' })
const meteorUri = `${this.props.meteorServerProtocol}://${this.props.meteorServerDomain}/_oauth/${this.props.provider.toLowerCase()}?${parsedUrl[1]}`
fetch(meteorUri)
.then((res) => res.text())
.then((text) => {
let $ = cheerio.load(text)
const config = JSON.parse($('#config').text())
Meteor._login({ oauth: {
credentialToken: config.credentialToken,
credentialSecret: config.credentialSecret
}}, (err, res) => {
if (err) console.error('Error', err)
this.setState({ stage: 'initial' })
})
})
.catch((err) => {
console.error('Error', err)
this.setState({ stage: 'initial' })
})
}, 500)
}
}
renderStage = (stage, user) => {
if (user) {
return (<View style={this.styles.view}>
<Text style={this.styles.text}>Logged in as {user.profile.name}.</Text>
<View style={this.styles.buttonContainer}>
<TouchableOpacity onPress={() => Meteor.logout()} style={this.styles.buttonLogout}>
<Text style={this.styles.buttonLogoutText}>Logout</Text>
</TouchableOpacity>
</View>
</View>)
}
return {
initial: (
<View style={this.styles.buttonContainer}>
<TouchableOpacity onPress={this.login} style={this.styles.buttonLogin}>
<Text style={this.styles.buttonLoginText}>Login with {this.props.provider}</Text>
</TouchableOpacity>
</View>),
webView: (
<Modal
animationType='fade'
onRequestClose={() => this.setState({ stage: 'initial' })}
visible={!!this.state.targetUrl}
>
<WebView
source={{uri: this.state.targetUrl}}
onNavigationStateChange={this.navigation}
/>
</Modal>
),
confirmation: (<ActivityIndicator animating size='large' />)
}[stage]
}
render () {
return (
<View style={this.styles.view}>
{this.renderStage(this.state.stage, this.props.user)}
</View>
)
}
}
function stateParam (loginStyle, credentialToken, redirectUrl) {
var state = {
loginStyle: loginStyle,
credentialToken: credentialToken
}
if (loginStyle === 'redirect') state.redirectUrl = redirectUrl || null
return Base64.encode(JSON.stringify(state))
}
export default createContainer((params) => {
return {
user: Meteor.user()
}
}, Login)