Android
Users
안드로이드 앱 사용자 등록 및 로그인 구현 튜토리얼
17 분
로그인 및 사용자 등록 튜토리얼 소개 이 섹션에서는 back4app을 사용하여 간단한 사용자 등록으로 앱을 만드는 방법을 살펴보겠습니다 parse server 핵심 기능 이 튜토리얼은 buildtoolsversion=30 0 2 buildtoolsversion=30 0 2 로 android studio 4 1 1에서 생성된 기본 앱을 사용합니다 compile sdk version = 30 0 2 compile sdk version = 30 0 2 및 targetsdkversion 30 targetsdkversion 30 언제든지 이 튜토리얼로 구축된 전체 android 프로젝트에 대한 액세스를 github 리포지토리에서 확인할 수 있습니다 kotlin 예제 리포지토리 java 예제 리포지토리 목표 parse를 사용하여 로그인하고 등록하는 방법을 배울 것입니다 우리가 달성할 목표의 미리보기입니다 전제 조건 이 튜토리얼을 완료하기 위해 우리는 다음이 필요합니다 안드로이드 스튜디오 back4app에서 생성된 앱 참고 새 파스 앱 튜토리얼 을 따라 back4app에서 parse 앱을 만드는 방법을 배우세요 back4app에 연결된 안드로이드 앱 참고 parse sdk 설치 튜토리얼 을 따라 back4app에 연결된 안드로이드 스튜디오 프로젝트를 만드세요 안드로이드 4 1 (젤리빈) 이상에서 실행되는 장치 (또는 가상 장치 ) 1 라이브러리 가져오기 이 단계에서는 프로젝트에서 사용할 라이브러리를 가져옵니다 우리는 다음 parse 클래스를 우리의 활동에 추가할 것입니다 2\ 우리는 프로젝트에서 람다 함수를 자주 사용할 것이므로 build gradle(module\ app)을 통해 java 1 8을 추가해야 합니다 2 회원가입 회원가입은 기본적으로 user 클래스에 새로운 parse user 객체를 생성하며, 앱에서는 “user”로 표시됩니다 대시보드 대시보드 새로운 사용자를 생성할 때는 최소한 두 가지 속성을 설정해야 합니다 => parseuser setusername() parseuser setusername() 및 parseuser setpassword() parseuser setpassword() 안드로이드에서 새로운 사용자를 저장하는 데 사용되는 메서드는 parseuser signupinbackground() parseuser signupinbackground() , 이는 콜백 함수와 함께 제공될 수 있습니다 참고 이 특별한 클래스의 객체는 대시보드 대시보드 에 parseobject save() parseobject save() 메서드로 저장되지 않습니다 작동하게 하려면 signupactivity signupactivity 다음 단계를 따르세요 가져오기 당신의 signupactivity signupactivity , 1단계 에서 가져온 종속성과 함께 2\ 사용자 등록을 구현하려면, 다음 코드를 사용하세요 oncreate() oncreate() 메서드에서 1 parseuser user = new parseuser(); 2 // set the user's username and password, which can be obtained by a forms 3 user setusername( "\<your username here>"); 4 user setpassword( "\<your password here>"); 5 user signupinbackground(new signupcallback() { 6 @override 7 public void done(parseexception e) { 8 if (e == null) { 9 showalert("successful sign up!", "welcome" + "\<your username here>" +"!"); 10 } else { 11 parseuser logout(); 12 toast maketext(signupactivity this, e getmessage(), toast length long) show(); 13 } 14 } 15 });1 val user = parseuser(); 2 // set the user's username and password, which can be obtained by a forms 3 user setusername("\<your username here>"); 4 user setpassword("\<your password here>"); 5 user signupinbackground(signupcallback() { 6 if (it == null) { 7 showalert("successful sign up!", "welcome" + "\<your username here>" + "!"); 8 } else { 9 parseuser logout(); 10 toast maketext(this, it message, toast length long) show(); 11 } 12 }); 예제 프로젝트에서 이 코드는 회원가입 회원가입 버튼 콜백 안에 배치됩니다 또한, 사용자 이름과 비밀번호는 텍스트 편집 을 사용하여 가져옵니다 3\ 경고 대화 상자를 표시하는 추가 메서드를 추가하면 프로세스가 더 전문적으로 보이는 것이 흥미롭습니다 아래 메서드는 이를 수행합니다 1 private void showalert(string title,string message){ 2 alertdialog builder builder = new alertdialog builder(signupactivity this) 3 settitle(title) 4 setmessage(message) 5 setpositivebutton("ok", new dialoginterface onclicklistener() { 6 @override 7 public void onclick(dialoginterface dialog, int which) { 8 dialog cancel(); 9 // don't forget to change the line below with the names of your activities 10 intent intent = new intent(signupactivity this, logoutactivity class); 11 intent addflags(intent flag activity clear task | intent flag activity new task); 12 startactivity(intent); 13 } 14 }); 15 alertdialog ok = builder create(); 16 ok show(); 17 }1 private fun showalert(title string, message string) { 2 val builder = alertdialog builder(this) 3 settitle(title) 4 setmessage(message) 5 setpositivebutton("ok") { dialog, which > 6 dialog cancel() 7 // don't forget to change the line below with the names of your activities 8 val intent = intent(this, logoutactivity class java) 9 intent addflags(intent flag activity clear task or intent flag activity new task) 10 startactivity(intent) 11 } 12 val ok = builder create() 13 ok show() 14 } 3 로그인 로그인은 세션 객체를 생성하며, 이는 로그인한 사용자를 가리킵니다 로그인에 성공하면, parseuser getcurrentuser() parseuser getcurrentuser() 는 사용자 객체와 대시보드 대시보드 에서 생성된 세션 객체를 반환합니다 그렇지 않으면, 대상 사용자 이름이 존재하지 않거나 비밀번호가 잘못된 경우 null을 반환합니다 로그인 작업을 수행하는 데 사용되는 메서드는 parseuser logininbackground() parseuser logininbackground() , 이는 사용자 이름과 비밀번호 문자열만큼 많은 인수를 요구하며, 콜백 함수를 호출할 수 있습니다 참고 회원가입 후 자동으로 로그인이 수행됩니다 to make loginactivity loginactivity work, follow these steps import into your loginactivity loginactivity , in addition to the dependencies imported in the step 1 2\ to implement user login function, simply use the code 1 private void login(string username, string password) { 2 progressdialog show(); 3 parseuser logininbackground(username, password, (parseuser, e) > { 4 progressdialog dismiss(); 5 if (parseuser != null) { 6 showalert("successful login", "welcome back " + username + " !"); 7 } else { 8 parseuser logout(); 9 toast maketext(loginactivity this, e getmessage(), toast length long) show(); 10 } 11 }); 12 }1 fun login(username string, password string) { 2 progressdialog? show() 3 parseuser logininbackground(username,password) { parseuser parseuser?, parseexception parseexception? > 4 progressdialog? dismiss() 5 if (parseuser != null) { 6 showalert("successful login", "welcome back " + username + " !") 7 } else { 8 parseuser logout() 9 if (parseexception != null) { 10 toast maketext(this, parseexception message, toast length long) show() 11 } 12 } 13 } 14 } in the example project, this code is placed inside a log in log in button callback also, username and password are caught using edit texts the method showalert showalert is the same that you added in the signupactivity signupactivity , don’t forget to change its intent intent arguments though 4 로그 아웃 로그 아웃은 로그인한 사용자의 활성 세션 객체를 삭제합니다 로그 아웃을 수행하는 데 사용되는 방법은 parseuser logoutinbackground() parseuser logoutinbackground() 입니다 사용자 로그 아웃을 구현하려면 아래 코드를 사용하면 됩니다 logoutactivity logoutactivity 1 parseuser logoutinbackground(e > { 2 progressdialog dismiss(); 3 if (e == null) 4 showalert("so, you're going ", "ok bye bye then"); 5 });1 fun login(username string, password string) { 2 progressdialog? show() 3 parseuser logininbackground(username,password) { parseuser parseuser?, parseexception parseexception? > 4 progressdialog? dismiss() 5 if (parseuser != null) { 6 showalert("successful login", "welcome back " + username + " !") 7 } else { 8 parseuser logout() 9 if (parseexception != null) { 10 toast maketext(this, parseexception message, toast length long) show() 11 } 12 } 13 } 14 } 예제 프로젝트에서 이 코드는 로그 아웃 로그 아웃 버튼 콜백 안에 배치됩니다 메서드 showalert showalert 는 loginactivity loginactivity 와 signupactivity signupactivity 에 추가한 것과 동일합니다 그러나 intent intent 인수는 변경하는 것을 잊지 마세요 5 앱 테스트하기 앱을 실행하고 몇 명의 사용자를 생성한 후, 등록한 후 다시 로그인해 보세요 다음에서 로그인하세요 back4app 웹사이트 앱을 찾아서 대시보드 대시보드 > 코어 코어 > 브라우저 브라우저 > 사용자 사용자 이 시점에서 아래와 같이 사용자를 볼 수 있어야 합니다 참고 위에 표시된 코드를 사용하여 사용자가 로그인할 때마다 세션 세션 이 대시보드 대시보드 에서 열립니다 그러나 사용자가 로그아웃하면 해당 세션 세션 이 종료됩니다 또한, 로그인 또는 가입 시도가 실패할 때마다 parse server 대시보드 대시보드 에서 열린 세션 세션 이 삭제됩니다 완료되었습니다! 축하합니다! 이제 back4app을 통해 parse server의 핵심 기능을 사용하여 앱에 로그인하고, 등록하고, 로그아웃할 수 있습니다!