-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathsocial.Rmd
More file actions
183 lines (137 loc) · 4.08 KB
/
Copy pathsocial.Rmd
File metadata and controls
183 lines (137 loc) · 4.08 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
---
title: "Social Sites"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Social Sites}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
```
Google Firebase integrates nicely with multiple social sites so users can easily login via:
* Google
* Github
* Facebook
* Twitter
## Console
First, these need to be setup in the Firebase console. If you followed the [get started guide](get-started.html) you should already have enabled Google. Now we'll setup Github!
1. Head over to the [Firebase console](https://console.firebase.google.com/)
2. Go to your project
3. Open the "Authentication" <i class="fa fa-users"></i> in the top left
4. Visit the second tab "Sign-in method"
5. Select "Github"
This should present you with the screen below.

It prompts you to enable it and enter your app id and app secret; _notice the redirect url at the bottom of that box_. These are taken from a Github OAuth application: follow [this link](https://github.com/settings/applications/new) to create yours and fill in the form.
<div class="alert alert-warning">
<h4>Important</h4>
<p style="color:white;">Make sure you enter the "Authorization callback URL" given to you by Firebase which resembles `https://my-application.firebaseapp.com/__/auth/handler`.</p>
</div>

## Package
Once setup in the Firebase console you can use those social services in your shiny app. Let's create a basic shiny app that prompts the user to login on load; we'll provide . Remember to import the dependencies in the UI with `useFirebase`.
```r
library(shiny)
library(firebase)
# define signin
signin <- modalDialog(
title = "Login",
actionButton("google", "Google", icon = icon("google"), class = "btn-danger"),
actionButton("github", "Github", icon = icon("github")),
footer = NULL
)
ui <- fluidPage(
useFirebase() # import dependencies
)
server <- function(input, output) {
showModal(signin)
}
shinyApp(ui, server)
```
We are then going to initialise the authentication from the `FirebaseSocial` object.
```r
library(shiny)
library(firebase)
# define signin
signin <- modalDialog(
title = "Login",
actionButton("google", "Google", icon = icon("google"), class = "btn-danger"),
actionButton("github", "Github", icon = icon("github")),
footer = NULL
)
ui <- fluidPage(
useFirebase()
)
server <- function(input, output) {
showModal(signin)
f <- FirebaseSocial$new()
}
shinyApp(ui, server)
```
Finally, we can bind our buttons to actual sign in triggers. We do so with the different `launch_*` methods.
```r
library(shiny)
library(firebase)
# define signin
signin <- modalDialog(
title = "Login",
actionButton("google", "Google", icon = icon("google"), class = "btn-danger"),
actionButton("github", "Github", icon = icon("github")),
footer = NULL
)
ui <- fluidPage(
useFirebase()
)
server <- function(input, output) {
showModal(signin)
f <- FirebaseSocial$new()
observeEvent(input$google, {
f$launch_google()
})
observeEvent(input$github, {
f$launch_github()
})
}
shinyApp(ui, server)
```

Then again, we use the method `req_sign_in` to indicate observers and output that require the user to be logged in. Below we use the latter to remove the modal and display a plot.
```r
library(shiny)
library(firebase)
# define signin
signin <- modalDialog(
title = "Login",
actionButton("google", "Google", icon = icon("google"), class = "btn-danger"),
actionButton("github", "Github", icon = icon("github")),
footer = NULL
)
ui <- fluidPage(
useFirebase(),
plotOutput("plot")
)
server <- function(input, output) {
showModal(signin)
f <- FirebaseSocial$new()
observeEvent(input$google, {
f$launch_google()
})
observeEvent(input$github, {
f$launch_github()
})
observe({
f$req_sign_in()
removeModal()
})
output$plot <- renderPlot({
f$req_sign_in()
plot(cars)
})
}
shinyApp(ui, server)
```
