Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
elm-stuff/
build/
docs.json
example/Example.js
10 changes: 10 additions & 0 deletions example/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<html>
<body>
<script src="https://www.gstatic.com/firebasejs/3.6.9/firebase.js"></script>
<script src="./Example.js"></script>
<div id="main"></div>
<script>
Elm.Main.embed(document.getElementById('main'));
</script>
</body>
</html>
32 changes: 27 additions & 5 deletions example/src/Example.elm
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

module Main exposing (..)

import Html exposing (Html, div, input, output, label, text, a)
import Html.Events exposing (on, targetValue)
import Html exposing (Html, div, input, output, label, text, a, button)
import Html.Events exposing (on, targetValue, onClick)
import Html.Attributes exposing (href, target)
import Html.App
import Task exposing (Task)
Expand All @@ -28,8 +28,15 @@ import ElmFire.LowLevel
, Snapshot
, Subscription
, Error
, Location
)
import ElmFire
import ElmFire.Auth.LowLevel
exposing
( authenticate
, rememberDefault
, withPassword
)


-- Firebase location to access:
Expand All @@ -38,7 +45,12 @@ import ElmFire

firebaseUrl : String
firebaseUrl =
"https://elmfire-test.firebaseio.com/example"
"https://elmfire-test.firebaseio.com/"


location : Location
location =
(fromUrl firebaseUrl "example")


main : Program Never
Expand All @@ -63,6 +75,7 @@ type Msg
= Send String
| Sent (Result Error ())
| ValueChanged (Result Error Snapshot)
| Login


init : ( Model, Cmd Msg )
Expand All @@ -80,7 +93,7 @@ update msg model =
, Task.perform
(Sent << Err)
(Sent << Ok << (always ()))
(set (JE.string text) (fromUrl firebaseUrl))
(set (JE.string text) location)
)

Sent result ->
Expand All @@ -98,10 +111,18 @@ update msg model =
Err _ ->
( model, Cmd.none )

Login ->
( model
, Task.perform
(Sent << Err)
(Sent << Ok << (always ()))
(authenticate location [ rememberDefault ] (withPassword "[email protected]" "123test"))
)


subscriptions : Model -> Sub Msg
subscriptions model =
ElmFire.valueChanged (fromUrl firebaseUrl) ValueChanged
ElmFire.valueChanged location ValueChanged


view : Model -> Html Msg
Expand All @@ -121,4 +142,5 @@ view model =
, output [] [ text model ]
]
]
, button [ onClick Login ] [ text "Login" ]
]
7 changes: 7 additions & 0 deletions example/start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env bash

set -e

elm make src/Example.elm --output Example.js

open index.html
1 change: 0 additions & 1 deletion src/ElmFire/Auth/LowLevel.elm
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ Therefore, only the root of the `Location` parameter is relevant.
createUser, removeUser, changeEmail, changePassword, resetPassword
-}

import Native.Firebase
import Native.ElmFire.Auth
import ElmFire.LowLevel exposing (Location, Reference, Error)
import Date exposing (Date)
Expand Down
17 changes: 3 additions & 14 deletions src/ElmFire/LowLevel.elm
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ module ElmFire.LowLevel
, open
, key
, toUrl
, location
, Priority(..)
, set
, setWithPriority
Expand Down Expand Up @@ -119,7 +118,6 @@ ElmFire maps the Firebase JavaScript API to Elm functions and tasks.
-}

import ElmFire.Types exposing (..)
import Native.Firebase
import Native.ElmFire
import Time exposing (Time)
import Json.Encode as JE
Expand Down Expand Up @@ -268,9 +266,9 @@ type Action

loc = fromUrl "https://elmfire.firebaseio-demo.com/foo/bar"
-}
fromUrl : String -> Location
fromUrl url =
Location [ ( "url", url ) ]
fromUrl : String -> String -> Location
fromUrl url ref =
Location [ ( "ref", ref ), ( "url", url ) ]


{-| Construct a location for the descendant at the specified relative path.
Expand Down Expand Up @@ -317,15 +315,6 @@ push (Location list) =
Location (( "push", "" ) :: list)


{-| Obtain a location from a reference.

reference = location loc
-}
location : Reference -> Location
location ref =
fromUrl <| Native.ElmFire.toUrl ref


{-| Get the url of a reference.
-}
toUrl : Reference -> String
Expand Down
14 changes: 10 additions & 4 deletions src/Native/ElmFire.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,13 @@ var _ThomasWeiser$elmfire$Native_ElmFire = function () {
var rest = locationList._1;
switch (head._0) {
case 'url':
ref = new Firebase (head._1);
if (!firebase.apps.length) {
firebase.initializeApp({databaseURL: head._1});
}
ref = firebase.database();
break;
case 'ref':
ref = getRefStep (rest) .ref (head._1);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the spaces after function names?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

idk, that's the way thomas was doing ¯\_(ツ)_/¯

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's my style for JS code.
I would appreciate if contributors would stick the style of the existing code.

break;
case 'child':
ref = getRefStep (rest) .child (head._1);
Expand Down Expand Up @@ -499,15 +505,15 @@ var _ThomasWeiser$elmfire$Native_ElmFire = function () {
function setOffline (off) {
return _elm_lang$core$Native_Scheduler .nativeBinding (function (callback) {
if (off) {
Firebase.goOffline ();
firebase.database().goOffline ();
} else {
Firebase.goOnline ();
firebase.database().goOnline ();
}
callback (_elm_lang$core$Native_Scheduler.succeed (_elm_lang$core$Native_Utils.Tuple0));
});
}

var serverTimeStamp = Firebase.ServerValue.TIMESTAMP;
var serverTimeStamp = firebase.database.ServerValue.TIMESTAMP;

return {
// Values exported to Elm
Expand Down
5 changes: 3 additions & 2 deletions src/Native/ElmFire/Auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ var _ThomasWeiser$elmfire$Native_ElmFire_Auth = function () {

function authenticate (location, listOfOptions, id) {
return _elm_lang$core$Native_Scheduler .nativeBinding (function (callback) {
var ref = getRef (location, callback);
getRef (location, callback);
var ref = firebase.auth();
if (ref) {
var options = toObject (listOfOptions);
var onComplete = function (err, auth) {
Expand All @@ -154,7 +155,7 @@ var _ThomasWeiser$elmfire$Native_ElmFire_Auth = function () {
ref.authAnonymously (onComplete, options);
break;
case 'Password':
ref.authWithPassword ({ email: id._0, password: id._1 }, onComplete, options);
ref.signInWithEmailAndPassword (id._0, id._1) .then (onComplete);
break;
case 'OAuthPopup':
ref.authWithOAuthPopup (id._0, onComplete, options);
Expand Down
Loading