From bbd8255288db31f4473b000aab2dfbe3d7fb0f38 Mon Sep 17 00:00:00 2001 From: Ognjen Cirkovic Date: Fri, 4 Nov 2022 23:33:50 +0100 Subject: Napravljen controller za refreshJwt-a na backu. --- Backend/Api/Api/Controllers/AuthController.cs | 11 +++++++++++ Backend/Api/Api/Interfaces/IUserService.cs | 2 +- Backend/Api/Api/Services/UserService.cs | 4 ++-- 3 files changed, 14 insertions(+), 3 deletions(-) (limited to 'Backend') diff --git a/Backend/Api/Api/Controllers/AuthController.cs b/Backend/Api/Api/Controllers/AuthController.cs index cbd5eb8..abb7adc 100644 --- a/Backend/Api/Api/Controllers/AuthController.cs +++ b/Backend/Api/Api/Controllers/AuthController.cs @@ -37,6 +37,17 @@ namespace Api.Controllers return Ok(); } + [HttpPost("refreshJwt")] + [Authorize(Roles ="User")] + public async Task> refreshJwt() + { + var jwt = await _userService.RenewToken(); + if (jwt != null) + { + return Ok(jwt); + } + return BadRequest("Pogresno uneti podaci"); + } [HttpPost("login")] public async Task> Login([FromBody] Login creds) { diff --git a/Backend/Api/Api/Interfaces/IUserService.cs b/Backend/Api/Api/Interfaces/IUserService.cs index 218c67a..db2eac1 100644 --- a/Backend/Api/Api/Interfaces/IUserService.cs +++ b/Backend/Api/Api/Interfaces/IUserService.cs @@ -12,7 +12,7 @@ namespace Api.Interfaces Task deleteUser(String email); Task getUserById(string id); - Task RenewToken(string existingToken); + Task RenewToken(); Task Login(Login login); Task Register(Register register); Task VerifyUser(VerifyUser login); diff --git a/Backend/Api/Api/Services/UserService.cs b/Backend/Api/Api/Services/UserService.cs index 034c494..5fd61f6 100644 --- a/Backend/Api/Api/Services/UserService.cs +++ b/Backend/Api/Api/Services/UserService.cs @@ -163,9 +163,9 @@ namespace Api.Services return false; } - public async Task RenewToken(string existingToken) + public async Task RenewToken() { - var id = _jwtService.TokenToId(existingToken); + var id = await UserIdFromJwt(); if (id == null) return null; var user = await getUserById(id); -- cgit v1.2.3 From bf2b7accb869db760ee04c2f33f93f08164b8993 Mon Sep 17 00:00:00 2001 From: "branislav.radivojevic" Date: Sat, 5 Nov 2022 14:51:17 +0100 Subject: reset sifre na app-u --- Backend/Api/Api/Services/PostService.cs | 10 +--- .../Activities/ActivityForgottenPassword.kt | 56 ++++++++++++++++++++-- .../Activities/ActivityForgottenPasswordVerify.kt | 56 ++++++++++++++++++++-- .../example/brzodolokacije/Interfaces/IAuthApi.kt | 7 +++ .../brzodolokacije/Models/Auth/ResetPass.kt | 4 ++ 5 files changed, 115 insertions(+), 18 deletions(-) create mode 100644 Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Models/Auth/ResetPass.kt (limited to 'Backend') diff --git a/Backend/Api/Api/Services/PostService.cs b/Backend/Api/Api/Services/PostService.cs index 2f29366..0a12f39 100644 --- a/Backend/Api/Api/Services/PostService.cs +++ b/Backend/Api/Api/Services/PostService.cs @@ -23,7 +23,7 @@ namespace Api.Services { Post p = new Post(); p._id = ""; - p.ownerId = _httpContext.HttpContext.User.FindFirstValue("id"); + p.ownerId = _httpContext.HttpContext.User.FindFirstValue("id").ToString(); p.locationId = post.locationId; p.description = post.description; @@ -63,14 +63,6 @@ namespace Api.Services } await _posts.InsertOneAsync(p); - - - - - - - - return postToPostSend(p); } diff --git a/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Activities/ActivityForgottenPassword.kt b/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Activities/ActivityForgottenPassword.kt index e7c9836..b0b7f5e 100644 --- a/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Activities/ActivityForgottenPassword.kt +++ b/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Activities/ActivityForgottenPassword.kt @@ -1,27 +1,73 @@ package com.example.brzodolokacije.Activities import android.content.Intent +import android.graphics.Color import androidx.appcompat.app.AppCompatActivity import android.os.Bundle +import android.util.Log import android.view.View import android.widget.Button +import android.widget.EditText import android.widget.Toast +import com.example.brzodolokacije.Models.Auth.JustMail +import com.example.brzodolokacije.Models.Auth.Login import com.example.brzodolokacije.R +import com.example.brzodolokacije.Services.RetrofitHelper +import com.example.brzodolokacije.Services.SharedPreferencesHelper +import okhttp3.ResponseBody +import retrofit2.Call +import retrofit2.Response class ActivityForgottenPassword : AppCompatActivity() { private lateinit var sendCode: Button + private lateinit var email: EditText + private lateinit var emailString:String + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_forgotten_password) sendCode=findViewById(R.id.forgottenPasswordSendCode) as Button - + email=findViewById(R.id.editTextTextPersonName) as EditText sendCode.setOnClickListener{ - intent= Intent(this, ActivityForgottenPasswordVerify::class.java) - startActivity(intent) - } + emailString=email.text.toString().trim() - } + if(!emailString.isEmpty() && checkEmail(emailString)==true) { + + var emailData= JustMail(emailString) + val authApi= RetrofitHelper.getInstance() + val request=authApi.forgotpass(emailData) + val cont=this + request.enqueue(object : retrofit2.Callback { + override fun onResponse(call: Call, response: Response) { + Log.d("main",response.code().toString()) + Log.d("main",response.body().toString()) + if(response.code()==200){ + val intent = Intent(cont, ActivityForgottenPasswordVerify::class.java) + intent.putExtra("email", emailString) + startActivity(intent) + } + } + override fun onFailure(call: Call, t: Throwable) { + } + }) + } + } + } + //from fragment login + fun checkEmail(emailString:String):Boolean{ + val emailRegex = "^[A-Za-z](.*)([@]{1})(.{1,})(\\.)(.{1,})" + if(!(emailRegex.toRegex().matches(emailString))){ + Toast.makeText( + this, "Email adresa nije validna, pokušajte ponovo", Toast.LENGTH_LONG + ).show(); + email.setHintTextColor(Color.RED) + return false + } + else{ + return true + } + } } \ No newline at end of file diff --git a/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Activities/ActivityForgottenPasswordVerify.kt b/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Activities/ActivityForgottenPasswordVerify.kt index 6533237..a1db97f 100644 --- a/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Activities/ActivityForgottenPasswordVerify.kt +++ b/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Activities/ActivityForgottenPasswordVerify.kt @@ -5,24 +5,72 @@ import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.view.View import android.widget.Button +import android.widget.EditText import android.widget.Toast import com.example.brzodolokacije.MainActivity +import com.example.brzodolokacije.Models.Auth.Login +import com.example.brzodolokacije.Models.Auth.ResetPass import com.example.brzodolokacije.R +import com.example.brzodolokacije.Services.RetrofitHelper +import com.example.brzodolokacije.Services.SharedPreferencesHelper +import okhttp3.ResponseBody +import retrofit2.Call +import retrofit2.Response class ActivityForgottenPasswordVerify : AppCompatActivity() { private lateinit var changePassword: Button + private lateinit var pw:EditText + private lateinit var pwchk:EditText + private lateinit var kod:EditText override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_forgotten_password_verify) + kod=findViewById(R.id.editTextTextPersonName) as EditText + pw=findViewById(R.id.editTextoldPassword) as EditText + pwchk =findViewById(R.id.editTextTextPassword) as EditText changePassword=findViewById(R.id.btnChangePassword) as Button changePassword.setOnClickListener{ + + var email =intent.getStringExtra("email") + var pwstr=pw.text.toString().trim() + var pwchkstr=pwchk.text.toString().trim() + var kodstr=kod.text.toString().trim() + + if(!kodstr.isEmpty() && checkPassword(pwstr,pwchkstr)){ + var resetData= ResetPass(email!!,kodstr,pwstr) + val authApi= RetrofitHelper.getInstance() + val request=authApi.resetpass(resetData) + val cont=this + request.enqueue(object : retrofit2.Callback { + override fun onResponse(call: Call, response: Response) { + if(response.code()==200){ + intent = Intent(cont, ActivityLoginRegister::class.java) + startActivity(intent) + } + } + override fun onFailure(call: Call, t: Throwable) { + } + }) + } + } + } + + //from fragment login + fun checkPassword(passwordString:String,passwordConfirm:String):Boolean{ + + if(passwordString.length<6){ Toast.makeText( - this, "Lozinka je uspešno promenjena.", Toast.LENGTH_LONG + this, "Lozinke su prekratke", Toast.LENGTH_LONG ).show(); - - intent= Intent(this, ActivityLoginRegister::class.java) - startActivity(intent) + return false + } + if(!passwordString.equals(passwordConfirm)){ + Toast.makeText( + this, "Lozinke su se ne poklapaju", Toast.LENGTH_LONG + ).show(); + return false } + return true } } \ No newline at end of file diff --git a/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Interfaces/IAuthApi.kt b/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Interfaces/IAuthApi.kt index 57e7e82..25f18a3 100644 --- a/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Interfaces/IAuthApi.kt +++ b/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Interfaces/IAuthApi.kt @@ -1,7 +1,9 @@ package com.example.brzodolokacije.Interfaces +import com.example.brzodolokacije.Models.Auth.JustMail import com.example.brzodolokacije.Models.Auth.Login import com.example.brzodolokacije.Models.Auth.Register +import com.example.brzodolokacije.Models.Auth.ResetPass import com.example.brzodolokacije.Models.Post import okhttp3.ResponseBody import retrofit2.Call @@ -16,6 +18,11 @@ interface IAuthApi { fun register(@Body obj:Register):Call @POST("/api/auth/refreshJwt") fun refreshJwt(@Header("Authorization") authHeader:String): Call + @POST("/api/auth/forgotpass") + fun forgotpass(@Body obj:JustMail):Call + @POST("/api/auth/resetpass") + fun resetpass(@Body obj:ResetPass):Call + //@POST("putanja") //fun add(@Body obj:Post,@Header("Authorization") authHeader:String):Call } \ No newline at end of file diff --git a/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Models/Auth/ResetPass.kt b/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Models/Auth/ResetPass.kt new file mode 100644 index 0000000..945c8d1 --- /dev/null +++ b/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Models/Auth/ResetPass.kt @@ -0,0 +1,4 @@ +package com.example.brzodolokacije.Models.Auth + +data class ResetPass(var email:String,var kod:String,var newpass:String) +data class JustMail(var email:String) \ No newline at end of file -- cgit v1.2.3 From f8b5a5eed843cdd0e94f96be072043f47c5db956 Mon Sep 17 00:00:00 2001 From: Ognjen Cirkovic Date: Sat, 5 Nov 2022 15:18:18 +0100 Subject: Kada se dodaje lokacija omoguceno da se koristi MapQuest geocoder da se nadju kordinate tog mesta i da se upisu u lokaciju. --- Backend/Api/Api/Api.csproj | 3 +++ Backend/Api/Api/Services/LocationService.cs | 18 ++++++++++++++++-- Backend/Api/Api/appsettings.json | 19 ++++++++++--------- 3 files changed, 29 insertions(+), 11 deletions(-) (limited to 'Backend') diff --git a/Backend/Api/Api/Api.csproj b/Backend/Api/Api/Api.csproj index b09c2fd..80898fd 100644 --- a/Backend/Api/Api/Api.csproj +++ b/Backend/Api/Api/Api.csproj @@ -7,6 +7,9 @@ + + + diff --git a/Backend/Api/Api/Services/LocationService.cs b/Backend/Api/Api/Services/LocationService.cs index 629c2a7..292fc0e 100644 --- a/Backend/Api/Api/Services/LocationService.cs +++ b/Backend/Api/Api/Services/LocationService.cs @@ -1,6 +1,11 @@ using Api.Interfaces; using Api.Models; +using Geocoding; +using Geocoding.Google; +using Geocoding.MapQuest; using MongoDB.Driver; +using ZstdSharp.Unsafe; +using Location = Api.Models.Location; namespace Api.Services { @@ -9,15 +14,24 @@ namespace Api.Services private readonly MongoClient _client; private readonly IMongoCollection _locations; private readonly IHttpContextAccessor _httpContext; - public LocationService(IDatabaseConnection settings, IMongoClient mongoClient) + private IConfiguration _configuration; + private MapQuestGeocoder _geocoder; + public LocationService(IDatabaseConnection settings, IMongoClient mongoClient, IConfiguration configuration) { var database = mongoClient.GetDatabase(settings.DatabaseName); _locations = database.GetCollection(settings.LocationCollectionName); + _configuration = configuration; + var _mapQuestApiKey = _configuration.GetSection("AppSettings:MapQuestApiKey").Value; + _geocoder = new MapQuestGeocoder(_mapQuestApiKey); + } public async Task add(Location loc) { - //TODO GOOGLE MAPS API CALL FOR info + IEnumerable
adresses = await _geocoder.GeocodeAsync(loc.name+" "+loc.address+" "+loc.city+" "+loc.country); + loc.latitude = adresses.First().Coordinates.Latitude; + loc.longitude=adresses.First().Coordinates.Longitude; await _locations.InsertOneAsync(loc); + return loc; } public async Task getById(string id) diff --git a/Backend/Api/Api/appsettings.json b/Backend/Api/Api/appsettings.json index b7f25b2..22d91dc 100644 --- a/Backend/Api/Api/appsettings.json +++ b/Backend/Api/Api/appsettings.json @@ -1,8 +1,9 @@ { - "AppSettings": { - "JwtToken": "PjrVqQJ1P2VOkuWLw7NaZUluT4z7bkau", - "EmailToken": "e8X8c0lm9KS7itWi3wgE6BiPXR21WPvO" - }, + "AppSettings": { + "JwtToken": "PjrVqQJ1P2VOkuWLw7NaZUluT4z7bkau", + "EmailToken": "e8X8c0lm9KS7itWi3wgE6BiPXR21WPvO", + "MapQuestApiKey": "47oeviBUoCI2JxWzNARmCtrH9fDp5Mtk" //msbs#556ASDFGGSGSD + }, "Logging": { "LogLevel": { @@ -21,11 +22,11 @@ "LocationCollectionname": "locations" }, - "EmailCfg": { - "Email": "oddyssey.brzodolokacije@gmail.com", - "SmtpServer": "smtp.gmail.com", - "Password": "nrokhfcwahfbqnpp" //msbs#556 - }, + "EmailCfg": { + "Email": "oddyssey.brzodolokacije@gmail.com", + "SmtpServer": "smtp.gmail.com", + "Password": "nrokhfcwahfbqnpp" //msbs#556 + }, "URLs": { "localhost": "http://localhost:5279/", "actual":"add url when back put onto server" -- cgit v1.2.3