Class: Firebase
- Inherits:
-
Object
- Object
- Firebase
- Defined in:
- lib/firebase/firebase_auth.rb,
lib/firebase/firebase.rb,
lib/firebase/firebase_github_helper.rb,
lib/firebase/firebase_twitter_helper.rb,
lib/firebase/firebase_facebook_helper.rb
Overview
see www.firebase.com/docs/ios/guide/login/facebook.html for more info (that’s where this code came from)
Constant Summary collapse
- ERRORS =
{ -9999 => :email_in_use, -2 => :wrong_password, }
Class Method Summary collapse
- .auth_data ⇒ Object
- .authenticate(token, options = {}, &block) ⇒ Object
- .authenticated?(&block) ⇒ Boolean
- .connected?(&block) ⇒ Boolean
- .convert_event_type(event_type) ⇒ Object
- .create_user(credentials, &block) ⇒ Object
- .create_user_and_login(credentials, &block) ⇒ Object
- .dispatch_queue=(queue) ⇒ Object
- .github_token ⇒ Object
- .github_token=(value) ⇒ Object
- .logout(&block) ⇒ Object
- .new(url = nil) ⇒ Object
-
.off_auth(handler) ⇒ Object
You should call this when you no longer need ‘authenticated?` data.
- .offline! ⇒ Object
- .online! ⇒ Object
- .open_facebook_session(options = {}, &block) ⇒ Object
- .open_github_session(options = {}, &block) ⇒ Object
- .open_twitter_session(options = {}, &block) ⇒ Object
- .remove_user(credentials, &block) ⇒ Object
- .send_password_reset(credentials, &block) ⇒ Object
- .twitter_api_key ⇒ Object
- .twitter_api_key=(value) ⇒ Object
- .update_user(credentials, &block) ⇒ Object
- .url ⇒ Object
- .url=(url) ⇒ Object
Instance Method Summary collapse
- #<<(value) ⇒ Object
- #[](*names) ⇒ Object
- #[]=(key, value) ⇒ Object
- #auth_data ⇒ Object
- #authenticate(token, options = {}, &and_then) ⇒ Object
-
#authenticated?(&block) ⇒ Boolean
checks the authenticated status.
- #cancel_disconnect(&and_then) ⇒ Object
- #child(name) ⇒ Object
- #clear!(&and_then) ⇒ Object
- #connected?(&block) ⇒ Boolean
- #connected_state(&block) ⇒ Object
- #create_user(credentials, &block) ⇒ Object
- #create_user_and_login(credentials, &block) ⇒ Object
- #inspect ⇒ Object
- #login(credentials, &block) ⇒ Object
- #login_anonymously(&block) ⇒ Object
- #login_to_facebook(credentials, &block) ⇒ Object
- #login_to_github(credentials, &block) ⇒ Object
- #login_to_google(credentials, &block) ⇒ Object
- #login_to_oauth(provider, parameters = {}, &block) ⇒ Object
- #login_to_twitter(credentials, &block) ⇒ Object
- #logout(&block) ⇒ Object
- #off_auth(handler) ⇒ Object
- #offline! ⇒ Object
- #on_disconnect(value, priority: priority, &and_then) ⇒ Object
- #online! ⇒ Object
- #open_facebook_session(options = {}, &block) ⇒ Object
- #open_github_session(options = {}, &block) ⇒ Object
- #open_twitter_session(options = {}, &block) ⇒ Object
- #priority(value, &and_then) ⇒ Object
- #priority=(value) ⇒ Object
- #push(value, &and_then) ⇒ Object
- #remove_on_disconnect(&and_then) ⇒ Object
- #remove_user(credentials, &block) ⇒ Object
- #send_password_reset(email, &block) ⇒ Object
- #set(value, priority: priority, &and_then) ⇒ Object
- #to_s ⇒ Object
- #transaction(options = {}, &transaction) ⇒ Object
- #update(values, &and_then) ⇒ Object
- #update_user(credentials, &block) ⇒ Object
- #value=(value) ⇒ Object
Class Method Details
.auth_data ⇒ Object
5 6 7 |
# File 'lib/firebase/firebase_auth.rb', line 5 def self.auth_data Firebase.new.auth_data end |
.authenticate(token, options = {}, &block) ⇒ Object
18 19 20 |
# File 'lib/firebase/firebase_auth.rb', line 18 def self.authenticate(token, ={}, &block) Firebase.new.authenticate(token, , &block) end |
.authenticated?(&block) ⇒ Boolean
46 47 48 |
# File 'lib/firebase/firebase_auth.rb', line 46 def self.authenticated?(&block) Firebase.new.authenticated?(&block) end |
.connected?(&block) ⇒ Boolean
77 78 79 |
# File 'lib/firebase/firebase.rb', line 77 def self.connected?(&block) Firebase.new.connected?(&block) end |
.convert_event_type(event_type) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/firebase/firebase.rb', line 7 def self.convert_event_type(event_type) case event_type when :child_added, :added, FEventTypeChildAdded return FEventTypeChildAdded when :child_moved, :moved, FEventTypeChildMoved return FEventTypeChildMoved when :child_changed, :changed, FEventTypeChildChanged return FEventTypeChildChanged when :child_removed, :removed, FEventTypeChildRemoved return FEventTypeChildRemoved when :value, FEventTypeValue return FEventTypeValue else NSLog("Unknown event type #{event_type.inspect}") end return event_type end |
.create_user(credentials, &block) ⇒ Object
68 69 70 |
# File 'lib/firebase/firebase_auth.rb', line 68 def self.create_user(credentials, &block) Firebase.new.create_user(credentials, &block) end |
.create_user_and_login(credentials, &block) ⇒ Object
87 88 89 |
# File 'lib/firebase/firebase_auth.rb', line 87 def self.create_user_and_login(credentials, &block) Firebase.new.create_user_and_login(credentials, &block) end |
.dispatch_queue=(queue) ⇒ Object
66 67 68 69 70 71 |
# File 'lib/firebase/firebase.rb', line 66 def self.dispatch_queue=(queue) if queue.is_a?(Dispatch::Queue) queue = queue.dispatch_object end setDispatchQueue(queue) end |
.github_token ⇒ Object
7 8 9 |
# File 'lib/firebase/firebase_github_helper.rb', line 7 def self.github_token @github_token end |
.github_token=(value) ⇒ Object
4 5 6 |
# File 'lib/firebase/firebase_github_helper.rb', line 4 def self.github_token=(value) @github_token = value end |
.logout(&block) ⇒ Object
35 36 37 |
# File 'lib/firebase/firebase_auth.rb', line 35 def self.logout(&block) Firebase.new.logout(&block) end |
.new(url = nil) ⇒ Object
53 54 55 56 57 58 59 60 61 |
# File 'lib/firebase/firebase.rb', line 53 def self.new(url=nil) if url.nil? @shared ||= alloc.initWithUrl(@url) elsif url alloc.initWithUrl(url) else super end end |
.off_auth(handler) ⇒ Object
You should call this when you no longer need ‘authenticated?` data.
61 62 63 |
# File 'lib/firebase/firebase_auth.rb', line 61 def self.off_auth(handler) Firebase.new.off_auth(handler) end |
.offline! ⇒ Object
90 91 92 |
# File 'lib/firebase/firebase.rb', line 90 def self.offline! Firebase.goOffline end |
.online! ⇒ Object
98 99 100 |
# File 'lib/firebase/firebase.rb', line 98 def self.online! Firebase.goOnline end |
.open_facebook_session(options = {}, &block) ⇒ Object
4 5 6 |
# File 'lib/firebase/firebase_facebook_helper.rb', line 4 def self.open_facebook_session(={}, &block) self.new.open_facebook_session(={}, &block) end |
.open_github_session(options = {}, &block) ⇒ Object
10 11 12 |
# File 'lib/firebase/firebase_github_helper.rb', line 10 def self.open_github_session(={}, &block) self.new.open_github_session(={}, &block) end |
.open_twitter_session(options = {}, &block) ⇒ Object
10 11 12 |
# File 'lib/firebase/firebase_twitter_helper.rb', line 10 def self.open_twitter_session(={}, &block) self.new.open_twitter_session(={}, &block) end |
.remove_user(credentials, &block) ⇒ Object
128 129 130 |
# File 'lib/firebase/firebase_auth.rb', line 128 def self.remove_user(credentials, &block) Firebase.new.remove_user(credentials, &block) end |
.send_password_reset(credentials, &block) ⇒ Object
121 122 123 |
# File 'lib/firebase/firebase_auth.rb', line 121 def self.send_password_reset(credentials, &block) Firebase.new.send_password_reset(credentials, &block) end |
.twitter_api_key ⇒ Object
7 8 9 |
# File 'lib/firebase/firebase_twitter_helper.rb', line 7 def self.twitter_api_key @twitter_api_key end |
.twitter_api_key=(value) ⇒ Object
4 5 6 |
# File 'lib/firebase/firebase_twitter_helper.rb', line 4 def self.twitter_api_key=(value) @twitter_api_key = value end |
.update_user(credentials, &block) ⇒ Object
108 109 110 |
# File 'lib/firebase/firebase_auth.rb', line 108 def self.update_user(credentials, &block) Firebase.new.update_user(credentials, &block) end |
.url ⇒ Object
49 50 51 |
# File 'lib/firebase/firebase.rb', line 49 def self.url @url end |
.url=(url) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/firebase/firebase.rb', line 25 def self.url=(url) if url.start_with?('http://') raise "Invalid URL #{url.inspect} in #{__method__}: URL scheme should be 'https://', not 'http://'" elsif url.start_with?('https://') # all good elsif url =~ %r'^\w+://' raise "Invalid URL #{url.inspect} in #{__method__}: URL scheme should be 'https://', not '#{$~}'" else url = "https://#{url}" end # should we support `Firebase.url = 'myapp/path/to/child/'` ? I'm gonna say # NO for now... unless url.include?('.firebaseio.com/') || url.include?('.firebaseio-demo.com/') after_scheme = url.index('//') + 2 if url[after_scheme..-1].include?('/') raise "Invalid URL #{url.inspect} in #{__method__}: URL does not include 'firebaseio.com'" end url = "#{url}.firebaseio.com/" end @url = url end |
Instance Method Details
#<<(value) ⇒ Object
158 159 160 161 162 |
# File 'lib/firebase/firebase.rb', line 158 def <<(value) ref = childByAutoId ref.set(value) return ref end |
#[](*names) ⇒ Object
133 134 135 136 137 138 139 |
# File 'lib/firebase/firebase.rb', line 133 def [](*names) if names.length == 0 childByAutoId else childByAppendingPath(names.join('/')) end end |
#[]=(key, value) ⇒ Object
145 146 147 |
# File 'lib/firebase/firebase.rb', line 145 def []=(key, value) childByAppendingPath(key).set(value) end |
#auth_data ⇒ Object
8 9 10 |
# File 'lib/firebase/firebase_auth.rb', line 8 def auth_data authData end |
#authenticate(token, options = {}, &and_then) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/firebase/firebase_auth.rb', line 21 def authenticate(token, ={}, &and_then) and_then ||= [:completion] disconnect_block = [:disconnect] if disconnect_block || and_then.arity < 2 NSLog('Warning! The Firebase authWithCredential method is deprecated.') NSLog('Instead of using a completion and cancel block, pass one block:') NSLog('fb.auth(token) do |error, auth_data| .. end') authWithCredential(token, withCompletionBlock:and_then, withCancelBlock: disconnect_block) else authWithCustomToken(token, withCompletionBlock: and_then) end return self end |
#authenticated?(&block) ⇒ Boolean
checks the authenticated status. If you pass a block the observeAuthEventWithBlock is used to determine the status. If you don’t pass a block, this method returns true or false.
52 53 54 55 56 57 58 |
# File 'lib/firebase/firebase_auth.rb', line 52 def authenticated?(&block) if block observeAuthEventWithBlock(block) else !!authData end end |
#cancel_disconnect(&and_then) ⇒ Object
214 215 216 217 218 219 220 221 |
# File 'lib/firebase/firebase.rb', line 214 def cancel_disconnect(&and_then) if block_given? cancelDisconnectOperationsWithCompletionBlock(and_then) else cancelDisconnectOperations end return self end |
#child(name) ⇒ Object
141 142 143 |
# File 'lib/firebase/firebase.rb', line 141 def child(name) childByAppendingPath(name) end |
#clear!(&and_then) ⇒ Object
149 150 151 152 153 154 155 156 |
# File 'lib/firebase/firebase.rb', line 149 def clear!(&and_then) if block_given? removeValueWithCompletionBlock(and_then) else removeValue end return self end |
#connected?(&block) ⇒ Boolean
80 81 82 83 84 85 86 87 88 |
# File 'lib/firebase/firebase.rb', line 80 def connected?(&block) if block connected_state.on(:value) do |snapshot| block.call(snapshot.value?) end else self.root['.info/connected'] end end |
#connected_state(&block) ⇒ Object
73 74 75 |
# File 'lib/firebase/firebase.rb', line 73 def connected_state(&block) connected? end |
#create_user(credentials, &block) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/firebase/firebase_auth.rb', line 71 def create_user(credentials, &block) raise ":email is required in #{__method__}" unless credentials.key?(:email) raise ":password is required in #{__method__}" unless credentials.key?(:password) email = credentials[:email] password = credentials[:password] begin if block && block.arity == 2 createUser(email, password: password, withValueCompletionBlock: block) else createUser(email, password: password, withCompletionBlock: block) end rescue RuntimeError => e block.call(e, nil) end end |
#create_user_and_login(credentials, &block) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/firebase/firebase_auth.rb', line 90 def create_user_and_login(credentials, &block) raise ":email is required in #{__method__}" unless credentials.key?(:email) raise ":password is required in #{__method__}" unless credentials.key?(:password) email = credentials[:email] password = credentials[:password] begin createUser(email, password: password, withCompletionBlock: -> (error) do if error block.call(error, nil) else login(credentials, &block) end end) rescue RuntimeError => e block.call(e, nil) end end |
#inspect ⇒ Object
261 262 263 |
# File 'lib/firebase/firebase.rb', line 261 def inspect "#<#{self.class}:0x#{self.object_id.to_s(16)}>" end |
#login(credentials, &block) ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/firebase/firebase_auth.rb', line 143 def login(credentials, &block) raise ":email is required in #{__method__}" unless credentials.key?(:email) raise ":password is required in #{__method__}" unless credentials.key?(:password) email = credentials[:email] password = credentials[:password] begin authUser(email, password: password, withCompletionBlock: block) rescue RuntimeError => e block.call(e, nil) end end |
#login_anonymously(&block) ⇒ Object
139 140 141 |
# File 'lib/firebase/firebase_auth.rb', line 139 def login_anonymously(&block) authAnonymouslyWithCompletionBlock(block) end |
#login_to_facebook(credentials, &block) ⇒ Object
176 177 178 179 180 181 182 183 184 185 |
# File 'lib/firebase/firebase_auth.rb', line 176 def login_to_facebook(credentials, &block) if credentials.is_a?(NSString) token = credentials else token = credentials[:token] end raise ":token is required in #{__method__}" unless token authWithOAuthProvider('facebook', token: token, withCompletionBlock: block) end |
#login_to_github(credentials, &block) ⇒ Object
201 202 203 204 205 206 207 208 209 210 |
# File 'lib/firebase/firebase_auth.rb', line 201 def login_to_github(credentials, &block) if credentials.is_a?(NSString) token = credentials else token = credentials[:token] end raise ":token is required in #{__method__}" unless token authWithOAuthProvider('github', token: token, withCompletionBlock: block) end |
#login_to_google(credentials, &block) ⇒ Object
212 213 214 215 216 217 218 219 220 221 |
# File 'lib/firebase/firebase_auth.rb', line 212 def login_to_google(credentials, &block) if credentials.is_a?(NSString) token = credentials else token = credentials[:token] end raise ":token is required in #{__method__}" unless token authWithOAuthProvider('google', token: token, withCompletionBlock: block) end |
#login_to_oauth(provider, parameters = {}, &block) ⇒ Object
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/firebase/firebase_auth.rb', line 155 def login_to_oauth(provider, parameters={}, &block) if parameters.is_a?(NSString) token = parameters elsif parameters.key?(:token) token = parameters[:token] else token = nil end if token authWithOAuthProvider(provider, token: token, withCompletionBlock: block) else objc_params = {} parameters.each do |key, value| # explicitly convert :sym to 'sym' objc_params[key.to_s] = value end authWithOAuthProvider(provider, parameters: objc_params, withCompletionBlock: block) end end |
#login_to_twitter(credentials, &block) ⇒ Object
187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/firebase/firebase_auth.rb', line 187 def login_to_twitter(credentials, &block) oauth_token = credentials[:token] || credentials[:oauth_token] oauth_token_secret = credentials[:secret] || credentials[:oauth_token_secret] raise ":token is required in #{__method__}" unless oauth_token raise ":secret is required in #{__method__}" unless oauth_token_secret provider = 'twitter' = { 'oauth_token' => oauth_token, 'oauth_token_secret' => oauth_token_secret, } login_to_oauth(provider, , &block) end |
#logout(&block) ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/firebase/firebase_auth.rb', line 38 def logout(&block) if block_given? unauthWithCompletionBlock(block) else unauth end end |
#off_auth(handler) ⇒ Object
64 65 66 |
# File 'lib/firebase/firebase_auth.rb', line 64 def off_auth(handler) removeAuthEventObserverWithHandle(handler) end |
#offline! ⇒ Object
94 95 96 |
# File 'lib/firebase/firebase.rb', line 94 def offline! Firebase.goOffline end |
#on_disconnect(value, priority: priority, &and_then) ⇒ Object
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/firebase/firebase.rb', line 227 def on_disconnect(value, &and_then) if block_given? if value.nil? onDisconnectRemoveValueWithCompletionBlock(and_then) elsif NSDictionary === value onDisconnectUpdateChildValues(value, withCompletionBlock: and_then) else onDisconnectSetValue(value, withCompletionBlock: and_then) end else if value.nil? onDisconnectRemoveValue elsif NSDictionary === value onDisconnectUpdateChildValues(value) else onDisconnectSetValue(value) end end return self end |
#online! ⇒ Object
102 103 104 |
# File 'lib/firebase/firebase.rb', line 102 def online! Firebase.goOnline end |
#open_facebook_session(options = {}, &block) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/firebase/firebase_facebook_helper.rb', line 8 def open_facebook_session(={}, &block) ref = self = [:permissions] || ['public_profile'] allow_ui = .fetch(:allow_login_ui, true) FBSession.openActiveSessionWithReadPermissions(, allowLoginUI: allow_ui, completionHandler: -> (session, state, error) do if error block.call(error, nil) elsif state == FBSessionStateOpen token = session.accessTokenData.accessToken ref.authWithOAuthProvider('facebook', token:token, withCompletionBlock:block) end end) nil end |
#open_github_session(options = {}, &block) ⇒ Object
14 15 16 17 18 19 |
# File 'lib/firebase/firebase_github_helper.rb', line 14 def open_github_session(={}, &block) ref = self token = [:token] || Firebase.github_token raise "token is required in #{__method__}" unless token firebase_ref.login_to_github('github', token) end |
#open_twitter_session(options = {}, &block) ⇒ Object
14 15 16 17 18 19 20 21 22 |
# File 'lib/firebase/firebase_twitter_helper.rb', line 14 def open_twitter_session(={}, &block) ref = self api_key = [:api_key] || Firebase.twitter_api_key raise "api_key is required in #{__method__}" unless api_key helper = Motion::Firebase::TwitterAuthHelper.new(ref, api_key) helper.select_twitter_account(&block) nil end |
#priority(value, &and_then) ⇒ Object
187 188 189 190 191 192 193 194 |
# File 'lib/firebase/firebase.rb', line 187 def priority(value, &and_then) if block_given? setPriority(value, withCompletionBlock: and_then) else setPriority(value) end return self end |
#priority=(value) ⇒ Object
183 184 185 |
# File 'lib/firebase/firebase.rb', line 183 def priority=(value) priority(value) end |
#push(value, &and_then) ⇒ Object
164 165 166 167 168 |
# File 'lib/firebase/firebase.rb', line 164 def push(value, &and_then) ref = childByAutoId ref.set(value, &and_then) return ref end |
#remove_on_disconnect(&and_then) ⇒ Object
223 224 225 |
# File 'lib/firebase/firebase.rb', line 223 def remove_on_disconnect(&and_then) on_disconnect(nil, &and_then) end |
#remove_user(credentials, &block) ⇒ Object
131 132 133 134 135 136 137 |
# File 'lib/firebase/firebase_auth.rb', line 131 def remove_user(credentials, &block) raise ":email is required in #{__method__}" unless credentials.key?(:email) raise ":password is required in #{__method__}" unless credentials.key?(:password) email = credentials[:email] password = credentials[:password] removeUser(email, password: password, withCompletionBlock: block) end |
#send_password_reset(email, &block) ⇒ Object
124 125 126 |
# File 'lib/firebase/firebase_auth.rb', line 124 def send_password_reset(email, &block) resetPasswordForUser(email, withCompletionBlock: block) end |
#set(value, priority: priority, &and_then) ⇒ Object
174 175 176 177 178 179 180 181 |
# File 'lib/firebase/firebase.rb', line 174 def set(value, &and_then) if block_given? setValue(value, withCompletionBlock: and_then) else setValue(value) end return self end |
#to_s ⇒ Object
257 258 259 |
# File 'lib/firebase/firebase.rb', line 257 def to_s description end |
#transaction(options = {}, &transaction) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/firebase/firebase.rb', line 106 def transaction(={}, &transaction) transaction = transaction || [:transaction] completion_block = [:completion] with_local_events = [:local] if with_local_events.nil? if block_given? runTransactionBlock(transaction, andCompletionBlock: completion_block) else runTransactionBlock(transaction) end else if block_given? runTransactionBlock(transaction, andCompletionBlock: completion_block, withLocalEvents: with_local_events) else runTransactionBlock(transaction, withLocalEvents: with_local_events) end end end |
#update(values, &and_then) ⇒ Object
205 206 207 208 209 210 211 212 |
# File 'lib/firebase/firebase.rb', line 205 def update(values, &and_then) if block_given? updateChildValues(values, withCompletionBlock: and_then) else updateChildValues(values) end return self end |
#update_user(credentials, &block) ⇒ Object
111 112 113 114 115 116 117 118 119 |
# File 'lib/firebase/firebase_auth.rb', line 111 def update_user(credentials, &block) raise ":email is required in #{__method__}" unless credentials.key?(:email) raise ":old_password is required in #{__method__}" unless credentials.key?(:old_password) raise ":new_password is required in #{__method__}" unless credentials.key?(:new_password) email = credentials[:email] old_password = credentials[:old_password] new_password = credentials[:new_password] changePasswordForUser(email, fromOld: old_password, toNew: new_password, withCompletionBlock: block) end |
#value=(value) ⇒ Object
170 171 172 |
# File 'lib/firebase/firebase.rb', line 170 def value=(value) setValue(value) end |