Skip to content

Instantly share code, notes, and snippets.

@yamacraft
Last active June 22, 2017 10:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yamacraft/c232359e28db4fcbca9ef2bd2420fe87 to your computer and use it in GitHub Desktop.
Save yamacraft/c232359e28db4fcbca9ef2bd2420fe87 to your computer and use it in GitHub Desktop.
2017-06-23 Otemachi Firebase #2 サンプルコード集

現在はFirebaseのサンプルをそのまま使っているので特に解説できることはない…。 https://github.com/firebase/quickstart-android/blob/master/auth/app/src/main/java/com/google/firebase/quickstart/auth/GoogleSignInActivity.java

Authenticationはあくまでも「認証後のtokenなどの情報を代わりに保管してくれる」機能の提供。認証そのものは自前で用意する必要がある(もしくはFirebase-UIを使うという手もある)。 今回は簡易的なUIで認証処理を用意したかったこともあり、Firebase-UIでは実装していない。

認証後はインスタンスを生成した時点で認証情報が取得できている。

mAuth = FirebaseAuth.getInstance();

// Google認証であればアカウントのメールアドレスが確認可能
String mail = mAuth.getCurrentUser().getEmail();

// 権限指定したDBをアクセスする際、特にUserやAuth情報を送る必要はない。
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference refs = database.getReference("/");
refs.child("ABC").setValue(data);
import com.google.firebase.database.IgnoreExtraProperties;
/**
* カードモデル
*/
@IgnoreExtraProperties
public class Card {
public String serial_id;
public String username;
public Card() {
}
public Card(String serial_id, String username) {
this.serial_id = serial_id;
this.username = username;
}
}
@Override
public void onRootReferenceDataChange(DataSnapshot dataSnapshot) {
mCards = new HashMap<>();
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
Card card = snapshot.getValue(Card.class);
if (card != null) {
mCards.put(card.serial_id, card);
}
}
if (mCallback != null) {
mCallback.onDatabaseDataChanged(mCards, new Date());
}
}
public class FirebaseDatabaseClient {
private static final String REFS_ROOT = BuildConfig.BUILD_TYPE + "/items";
private final DatabaseReference rootReference;
private Callback callback;
public FirebaseDatabaseClient() {
FirebaseDatabase database = FirebaseDatabase.getInstance();
this.rootReference = database.getReference(REFS_ROOT);
this.rootReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (callback != null) {
callback.onRootReferenceDataChange(dataSnapshot);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
if (callback != null) {
callback.onRootReferenceCancelled(databaseError);
}
}
});
}
public void setCallback(Callback callback) {
this.callback = callback;
}
/**
* カード情報追加
*
* @param serialId シリアルID
* @param card カード情報
*/
public void putCard(String serialId, Card card) {
this.rootReference.child(serialId).setValue(card);
}
public interface Callback {
void onRootReferenceDataChange(DataSnapshot dataSnapshot);
void onRootReferenceCancelled(DatabaseError databaseError);
}
}

データ構造

   (Root)/
      + Debug/
         + items/
            + User1(serial_id)/
               + user_name
               + serial_id
               + ...
            + User2/
               + ...
      + Release/
         + items/
            + ...

開発版とリリース(実運用版)でプロジェクトを分ける方が本当はいいのだけれど、管理のめんどくささがあるので同じプロジェクト内で作っています。

ルール

   {
     "rules": {
       ".read": "true",
       ".write": "auth.uid === '96zOf...'"
     }
   }

読み込みは(現時点で)秘匿するような情報はないので制限を設けていない。書き込みは特定のGoogleアカウントでログインしている端末のみ受け付ける。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment